tizen 2.4 release
[kernel/u-boot-tm1.git] / nand_fdl / fdl-2 / src / fdl_ubi.c
1 /*
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.
5  */
6
7 #include <common.h>
8 #include <exports.h>
9
10 #include <nand.h>
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>
16 #include "fdl_ubi.h"
17
18
19 struct ubi_selected_dev cur_ubi={0,-1,NULL};
20
21 static int verify_mkvol_req(const struct ubi_device *ubi,
22                             const struct ubi_mkvol_req *req)
23 {
24         int n, err = -EINVAL;
25
26         if (req->bytes < 0 || req->alignment < 0 || req->vol_type < 0 ||
27             req->name_len < 0)
28                 goto bad;
29
30         if ((req->vol_id < 0 || req->vol_id >= ubi->vtbl_slots) &&
31             req->vol_id != UBI_VOL_NUM_AUTO)
32                 goto bad;
33
34         if (req->alignment == 0)
35                 goto bad;
36
37         if (req->bytes == 0)
38                 goto bad;
39
40         if (req->vol_type != UBI_DYNAMIC_VOLUME &&
41             req->vol_type != UBI_STATIC_VOLUME)
42                 goto bad;
43
44         if (req->alignment > ubi->leb_size)
45                 goto bad;
46
47         n = req->alignment % ubi->min_io_size;
48         if (req->alignment != 1 && n)
49                 goto bad;
50
51         if (req->name_len > UBI_VOL_NAME_MAX) {
52                 err = -ENAMETOOLONG;
53                 goto bad;
54         }
55
56         return 0;
57 bad:
58         printf("bad volume creation request");
59         return err;
60 }
61
62 int fdl_ubi_create_vol(struct ubi_device *ubi, char *volume, int *vol_id, long long size, int dynamic)
63 {
64         struct ubi_mkvol_req req;
65         int err;
66
67         if (dynamic)
68                 req.vol_type = UBI_DYNAMIC_VOLUME;
69         else
70                 req.vol_type = UBI_STATIC_VOLUME;
71
72         req.vol_id = UBI_VOL_NUM_AUTO;
73         req.alignment = 1;
74         req.bytes = size;
75
76         strcpy(req.name, volume);
77         req.name_len = strlen(volume);
78         req.name[req.name_len] = '\0';
79         req.padding1 = 0;
80         /* It's duplicated at drivers/mtd/ubi/cdev.c */
81         err = verify_mkvol_req(ubi, &req);
82         if (err) {
83                 printf("verify_mkvol_req failed %d\n", err);
84                 return err;
85         }
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);
90         if(err){
91                 printf("Creat volume err %d\n",err);
92         }
93         *vol_id = req.vol_id;
94         return err;
95 }
96
97 int fdl_ubi_remove_vol(struct ubi_device *ubi, char *volume)
98 {
99         int i, err, reserved_pebs;
100         int found = 0, vol_id = 0;
101         struct ubi_volume *vol = NULL;
102
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);
107                         vol_id = i;
108                         found = 1;
109                         break;
110                 }
111         }
112         if (!found) {
113                 printf("%s volume not found\n", volume);
114                 return -ENODEV;
115         }
116         printf("remove UBI volume %s (id %d)\n", vol->name, vol->vol_id);
117
118         if (ubi->ro_mode) {
119                 printf("It's read-only mode\n");
120                 err = -EROFS;
121                 goto out_err;
122         }
123
124         err = ubi_change_vtbl_record(ubi, vol_id, NULL);
125         if (err) {
126                 printf("Error changing Vol tabel record err=%x\n", err);
127                 goto out_err;
128         }
129         reserved_pebs = vol->reserved_pebs;
130         for (i = 0; i < vol->reserved_pebs; i++) {
131                 err = ubi_eba_unmap_leb(ubi, vol, i);
132                 if (err)
133                         goto out_err;
134         }
135
136         kfree(vol->eba_tbl);
137         ubi->volumes[vol_id]->eba_tbl = NULL;
138         ubi->volumes[vol_id] = NULL;
139
140         ubi->rsvd_pebs -= reserved_pebs;
141         ubi->avail_pebs += reserved_pebs;
142         i = ubi->beb_rsvd_level - ubi->beb_rsvd_pebs;
143         if (i > 0) {
144                 i = ubi->avail_pebs >= i ? i : ubi->avail_pebs;
145                 ubi->avail_pebs -= i;
146                 ubi->rsvd_pebs += i;
147                 ubi->beb_rsvd_pebs += i;
148                 if (i > 0)
149                         ubi_msg("reserve more %d PEBs", i);
150         }
151         ubi->vol_count -= 1;
152
153         return 0;
154 out_err:
155         ubi_err("cannot remove volume %d, error %d", vol_id, err);
156         return err;
157 }
158
159 int fdl_ubi_volume_write(struct ubi_device *ubi, char *volume, void *buf, size_t size)
160 {
161         int i = 0, err = -1;
162         int found = 0;
163         struct ubi_volume *vol;
164
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);
169                         found = 1;
170                         break;
171                 }
172         }
173         if (!found) {
174                 printf("%s volume not found\n", volume);
175                 return 1;
176         }
177         
178         err = ubi_more_update_data(ubi, vol, buf, size);
179         if (err < 0) {
180                 printf("Couldnt or partially wrote data \n");
181                 return err;
182         }
183
184         if (err) {
185                 size = err;
186
187                 err = ubi_check_volume(ubi, vol->vol_id);
188                 if ( err < 0 )
189                         return err;
190
191                 if (err) {
192                         ubi_warn("volume %d on UBI device %d is corrupted",
193                                         vol->vol_id, ubi->ubi_num);
194                         vol->corrupted = 1;
195                 }
196
197                 vol->checked = 1;
198                 ubi_gluebi_updated(vol);
199         }
200
201         return 0;
202 }
203
204 int fdl_ubi_volume_read(struct ubi_device *ubi, char *volume, char *buf, size_t size, size_t offset)
205 {
206         int err, lnum, off, len, tbuf_size, i = 0;
207         size_t count_save = size;
208         void *tbuf;
209         unsigned long long tmp;
210         struct ubi_volume *vol = NULL;
211         loff_t offp = offset;
212
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);
218                         break;
219                 }
220         }
221         if (i == ubi->vtbl_slots) {
222                 printf("%s volume not found\n", volume);
223                 return -ENODEV;
224         }
225
226         printf("read %i bytes from volume %d to %x(buf address)\n",
227                (int) size, vol->vol_id, (unsigned)buf);
228
229         if (vol->updating) {
230                 printf("updating");
231                 return -EBUSY;
232         }
233         if (vol->upd_marker) {
234                 printf("damaged volume, update marker is set");
235                 return -EBADF;
236         }
237         if (offp == vol->used_bytes)
238                 return 0;
239
240         if (size == 0) {
241                 printf("Read [%lu] bytes\n", (unsigned long) vol->used_bytes);
242                 size = vol->used_bytes;
243         }
244
245         if (vol->corrupted)
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;
249
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);
254         if (!tbuf) {
255                 printf("NO MEM\n");
256                 return -ENOMEM;
257         }
258         len = size > tbuf_size ? tbuf_size : size;
259
260         tmp = offp;
261         off = do_div(tmp, vol->usable_leb_size);
262         lnum = tmp;
263         do {
264                 if (off + len >= vol->usable_leb_size)
265                         len = vol->usable_leb_size - off;
266
267                 err = ubi_eba_read_leb(ubi, vol, lnum, tbuf, off, len, 0);
268                 if (err) {
269                         printf("read err %x\n", err);
270                         break;
271                 }
272                 off += len;
273                 if (off == vol->usable_leb_size) {
274                         lnum += 1;
275                         off -= vol->usable_leb_size;
276                 }
277
278                 size -= len;
279                 offp += len;
280
281                 memcpy(buf, tbuf, len);
282
283                 buf += len;
284                 len = size > tbuf_size ? tbuf_size : size;
285         } while (size);
286
287         free(tbuf);
288         return err ? err : count_save - size;
289 }
290
291 /**
292         attach ubi device to given mtdpart, and return the new
293         ubi device num.
294 */
295 int fdl_ubi_dev_attach(const char *mtdpart)
296 {
297         struct mtd_device *dev;
298         struct part_info *part;
299         struct mtd_info *mtd;
300         struct mtd_partition mtd_part;
301         char mtd_dev[16];
302         u8 pnum;
303         int ret;
304
305         ret = find_dev_and_part(mtdpart, &dev, &pnum, &part);
306         if(ret){
307                 printf("--->main partition %s miss<---\n",mtdpart);
308                 return -1;
309         }
310         if(dev->id->type != MTD_DEV_TYPE_NAND){
311                 printf("mtd dev %s not a nand device!\n",mtdpart);
312                 return -1;
313         }
314         sprintf(mtd_dev, "%s%d", MTD_DEV_TYPE(dev->id->type), dev->id->num);
315         mtd = get_mtd_device_nm(mtd_dev);
316
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);
323
324         ret = ubi_attach_mtd_dev(mtd, UBI_DEV_NUM_AUTO, 0);
325         if(ret<0){
326                 printf("--->ubi attach mtd %s failed<---\n",mtdpart);
327         }
328         return ret;
329 }
330
331 /**
332  * init mtd ubi module and attach the UBIPAC part to ubi device.
333  *
334  * Returns 1 in case of success and 0 in case of failure.
335  */
336 int fdl_ubi_dev_init(void)
337 {
338         int ret;
339
340         if(cur_ubi.ubi_initialized){
341                 ubi_detach_mtd_dev(cur_ubi.dev_num, 1);
342         }else{
343                 //init mtd & ubi devices
344                 ret = mtdparts_init();
345                 if(ret){
346                         printf("--->mtdparts init error<---\n");
347                         return 0;
348                 }
349                 ret = ubi_init();
350                 if(ret){
351                         printf("--->ubi init error<---\n");
352                         return 0;
353                 }
354         }
355         
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__);
359                 return 0;
360         }
361         
362         cur_ubi.ubi_initialized =1;
363         
364         cur_ubi.dev = ubi_get_device(cur_ubi.dev_num);
365         if(!cur_ubi.dev){
366                 printf("%s:ubi get device failure!\n",__FUNCTION__);
367                 return 0;
368         }
369
370         return 1;
371 }
372
373 /**
374  * autoresize - re-size the volume to max.
375  * @ubi: UBI device description object
376  * @vol_id: ID of the volume to re-size
377  *
378  * Returns zero in case of success and a
379  * negative error code in case of failure.
380  */
381 int fdl_ubi_volume_autoresize(struct ubi_device *ubi, int vol_id)
382 {
383         struct ubi_volume_desc desc;
384         struct ubi_volume *vol = ubi->volumes[vol_id];
385         int err, old_reserved_pebs = vol->reserved_pebs;
386
387         desc.vol = vol;
388         err = ubi_resize_volume(&desc,
389                         old_reserved_pebs + ubi->avail_pebs);
390         if (err){
391                 printf("cannot auto-resize volume %d\n", vol_id);
392                 return err;
393         }
394
395         printf("volume %d (\"%s\") re-sized from %d to %d LEBs\n", vol_id,
396                 vol->name, old_reserved_pebs, vol->reserved_pebs);
397         return 0;
398 }
399
400 /**
401  * fdl_ubi_volume_start_update - prepare volume update.
402  * @ubi: UBI device description object
403  * @volume: volume description object
404  * @size: update bytes
405  *
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.
409  */
410 int fdl_ubi_volume_start_update(struct ubi_device *ubi, char *volume, size_t size)
411 {
412         int i = 0, err = -1;
413         int rsvd_bytes = 0;
414         int found = 0;
415         struct ubi_volume *vol;
416
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);
421                         found = 1;
422                         break;
423                 }
424         }
425         if (!found) {
426                 printf("%s volume not found\n", volume);
427                 return 1;
428         }
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");
435                 return 1;
436         }
437
438         err = ubi_start_update(ubi, vol, size);
439         if (err < 0) {
440                 printf("Cannot start volume update\n");
441                 return err;
442         }
443
444         return 0;
445 }
446