2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License version 2 as
4 * published by the Free Software Foundation.
11 #include <onenand_uboot.h>
12 #include <linux/mtd/mtd.h>
13 #include <linux/mtd/partitions.h>
14 #include <asm/errno.h>
15 #include <jffs2/load_kernel.h>
19 struct ubi_selected_dev cur_ubi={0,-1,NULL};
21 static int verify_mkvol_req(const struct ubi_device *ubi,
22 const struct ubi_mkvol_req *req)
26 if (req->bytes < 0 || req->alignment < 0 || req->vol_type < 0 ||
30 if ((req->vol_id < 0 || req->vol_id >= ubi->vtbl_slots) &&
31 req->vol_id != UBI_VOL_NUM_AUTO)
34 if (req->alignment == 0)
40 if (req->vol_type != UBI_DYNAMIC_VOLUME &&
41 req->vol_type != UBI_STATIC_VOLUME)
44 if (req->alignment > ubi->leb_size)
47 n = req->alignment % ubi->min_io_size;
48 if (req->alignment != 1 && n)
51 if (req->name_len > UBI_VOL_NAME_MAX) {
58 printf("bad volume creation request");
62 int fdl_ubi_create_vol(struct ubi_device *ubi, char *volume, int *vol_id, long long size, int dynamic)
64 struct ubi_mkvol_req req;
68 req.vol_type = UBI_DYNAMIC_VOLUME;
70 req.vol_type = UBI_STATIC_VOLUME;
72 req.vol_id = UBI_VOL_NUM_AUTO;
76 strcpy(req.name, volume);
77 req.name_len = strlen(volume);
78 req.name[req.name_len] = '\0';
80 /* It's duplicated at drivers/mtd/ubi/cdev.c */
81 err = verify_mkvol_req(ubi, &req);
83 printf("verify_mkvol_req failed %d\n", err);
86 printf("Creating %s volume %s of size 0x%llx\n",
87 dynamic ? "dynamic" : "static", volume, size);
88 /* Call real ubi create volume */
89 err = ubi_create_volume(ubi, &req);
91 printf("Creat volume err %d\n",err);
97 int fdl_ubi_remove_vol(struct ubi_device *ubi, char *volume)
99 int i, err, reserved_pebs;
100 int found = 0, vol_id = 0;
101 struct ubi_volume *vol = NULL;
103 for (i = 0; i < ubi->vtbl_slots; i++) {
104 vol = ubi->volumes[i];
105 if (vol && !strcmp(vol->name, volume)) {
106 printf("Volume %s found at valid %d\n", volume, i);
113 printf("%s volume not found\n", volume);
116 printf("remove UBI volume %s (id %d)\n", vol->name, vol->vol_id);
119 printf("It's read-only mode\n");
124 err = ubi_change_vtbl_record(ubi, vol_id, NULL);
126 printf("Error changing Vol tabel record err=%x\n", err);
129 reserved_pebs = vol->reserved_pebs;
130 for (i = 0; i < vol->reserved_pebs; i++) {
131 err = ubi_eba_unmap_leb(ubi, vol, i);
137 ubi->volumes[vol_id]->eba_tbl = NULL;
138 ubi->volumes[vol_id] = NULL;
140 ubi->rsvd_pebs -= reserved_pebs;
141 ubi->avail_pebs += reserved_pebs;
142 i = ubi->beb_rsvd_level - ubi->beb_rsvd_pebs;
144 i = ubi->avail_pebs >= i ? i : ubi->avail_pebs;
145 ubi->avail_pebs -= i;
147 ubi->beb_rsvd_pebs += i;
149 ubi_msg("reserve more %d PEBs", i);
155 ubi_err("cannot remove volume %d, error %d", vol_id, err);
159 int fdl_ubi_volume_write(struct ubi_device *ubi, char *volume, void *buf, size_t size)
163 struct ubi_volume *vol;
165 for (i = 0; i < ubi->vtbl_slots; i++) {
166 vol = ubi->volumes[i];
167 if (vol && !strcmp(vol->name, volume)) {
168 printf("Volume \"%s\" found at volume id %d\n", volume, i);
174 printf("%s volume not found\n", volume);
178 err = ubi_more_update_data(ubi, vol, buf, size);
180 printf("Couldnt or partially wrote data \n");
187 err = ubi_check_volume(ubi, vol->vol_id);
192 ubi_warn("volume %d on UBI device %d is corrupted",
193 vol->vol_id, ubi->ubi_num);
198 ubi_gluebi_updated(vol);
204 int fdl_ubi_volume_read(struct ubi_device *ubi, char *volume, char *buf, size_t size, size_t offset)
206 int err, lnum, off, len, tbuf_size, i = 0;
207 size_t count_save = size;
209 unsigned long long tmp;
210 struct ubi_volume *vol = NULL;
211 loff_t offp = offset;
213 for (i = 0; i < ubi->vtbl_slots; i++) {
214 vol = ubi->volumes[i];
215 if (vol && !strcmp(vol->name, volume)) {
216 printf("Volume %s found at volume id %d\n",
217 volume, vol->vol_id);
221 if (i == ubi->vtbl_slots) {
222 printf("%s volume not found\n", volume);
226 printf("read %i bytes from volume %d to %x(buf address)\n",
227 (int) size, vol->vol_id, (unsigned)buf);
233 if (vol->upd_marker) {
234 printf("damaged volume, update marker is set");
237 if (offp == vol->used_bytes)
241 printf("Read [%lu] bytes\n", (unsigned long) vol->used_bytes);
242 size = vol->used_bytes;
246 printf("read from corrupted volume %d", vol->vol_id);
247 if (offp + size > vol->used_bytes)
248 count_save = size = vol->used_bytes - offp;
250 tbuf_size = vol->usable_leb_size;
251 if (size < tbuf_size)
252 tbuf_size = ALIGN(size, ubi->min_io_size);
253 tbuf = malloc(tbuf_size);
258 len = size > tbuf_size ? tbuf_size : size;
261 off = do_div(tmp, vol->usable_leb_size);
264 if (off + len >= vol->usable_leb_size)
265 len = vol->usable_leb_size - off;
267 err = ubi_eba_read_leb(ubi, vol, lnum, tbuf, off, len, 0);
269 printf("read err %x\n", err);
273 if (off == vol->usable_leb_size) {
275 off -= vol->usable_leb_size;
281 memcpy(buf, tbuf, len);
284 len = size > tbuf_size ? tbuf_size : size;
288 return err ? err : count_save - size;
292 attach ubi device to given mtdpart, and return the new
295 int fdl_ubi_dev_attach(const char *mtdpart)
297 struct mtd_device *dev;
298 struct part_info *part;
299 struct mtd_info *mtd;
300 struct mtd_partition mtd_part;
305 ret = find_dev_and_part(mtdpart, &dev, &pnum, &part);
307 printf("--->main partition %s miss<---\n",mtdpart);
310 if(dev->id->type != MTD_DEV_TYPE_NAND){
311 printf("mtd dev %s not a nand device!\n",mtdpart);
314 sprintf(mtd_dev, "%s%d", MTD_DEV_TYPE(dev->id->type), dev->id->num);
315 mtd = get_mtd_device_nm(mtd_dev);
317 memset(&mtd_part, 0, sizeof(mtd_part));
318 mtd_part.name = mtdpart;
319 mtd_part.size = part->size;
320 mtd_part.offset = part->offset;
321 add_mtd_partitions(mtd, &mtd_part, 1);
322 mtd = get_mtd_device_nm(mtdpart);
324 ret = ubi_attach_mtd_dev(mtd, UBI_DEV_NUM_AUTO, 0);
326 printf("--->ubi attach mtd %s failed<---\n",mtdpart);
332 * init mtd ubi module and attach the UBIPAC part to ubi device.
334 * Returns 1 in case of success and 0 in case of failure.
336 int fdl_ubi_dev_init(void)
340 if(cur_ubi.ubi_initialized){
341 ubi_detach_mtd_dev(cur_ubi.dev_num, 1);
343 //init mtd & ubi devices
344 ret = mtdparts_init();
346 printf("--->mtdparts init error<---\n");
351 printf("--->ubi init error<---\n");
356 cur_ubi.dev_num = fdl_ubi_dev_attach(UBIPAC_PART);
357 if(cur_ubi.dev_num<0){
358 printf("%s:ubi dev attach failure!\n",__FUNCTION__);
362 cur_ubi.ubi_initialized =1;
364 cur_ubi.dev = ubi_get_device(cur_ubi.dev_num);
366 printf("%s:ubi get device failure!\n",__FUNCTION__);
374 * autoresize - re-size the volume to max.
375 * @ubi: UBI device description object
376 * @vol_id: ID of the volume to re-size
378 * Returns zero in case of success and a
379 * negative error code in case of failure.
381 int fdl_ubi_volume_autoresize(struct ubi_device *ubi, int vol_id)
383 struct ubi_volume_desc desc;
384 struct ubi_volume *vol = ubi->volumes[vol_id];
385 int err, old_reserved_pebs = vol->reserved_pebs;
388 err = ubi_resize_volume(&desc,
389 old_reserved_pebs + ubi->avail_pebs);
391 printf("cannot auto-resize volume %d\n", vol_id);
395 printf("volume %d (\"%s\") re-sized from %d to %d LEBs\n", vol_id,
396 vol->name, old_reserved_pebs, vol->reserved_pebs);
401 * fdl_ubi_volume_start_update - prepare volume update.
402 * @ubi: UBI device description object
403 * @volume: volume description object
404 * @size: update bytes
406 * This function starts volume update operation. If @size is zero, the volume
407 * is just wiped out. Returns zero in case of success and a negative error code
408 * in case of failure.
410 int fdl_ubi_volume_start_update(struct ubi_device *ubi, char *volume, size_t size)
415 struct ubi_volume *vol;
417 for (i = 0; i < ubi->vtbl_slots; i++) {
418 vol = ubi->volumes[i];
419 if (vol && !strcmp(vol->name, volume)) {
420 printf("Volume \"%s\" found at volume id %d\n", volume, i);
426 printf("%s volume not found\n", volume);
429 rsvd_bytes = vol->reserved_pebs * (ubi->leb_size - vol->data_pad);
430 if (size < 0 || size > rsvd_bytes) {
431 printf("rsvd_bytes=%d vol->reserved_pebs=%d ubi->leb_size=%d\n",
432 rsvd_bytes, vol->reserved_pebs, ubi->leb_size);
433 printf("vol->data_pad=%d\n", vol->data_pad);
434 printf("Size > volume size !!\n");
438 err = ubi_start_update(ubi, vol, size);
440 printf("Cannot start volume update\n");