Blame view

common/cmd_ubi.c 15.3 KB
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
1
2
3
4
5
6
  /*
   * Unsorted Block Image commands
   *
   *  Copyright (C) 2008 Samsung Electronics
   *  Kyungmin Park <kyungmin.park@samsung.com>
   *
2d579e506   Stefan Roese   ubi: Remove flash...
7
   * Copyright 2008-2009 Stefan Roese <sr@denx.de>, DENX Software Engineering
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
   *
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU General Public License version 2 as
   * published by the Free Software Foundation.
   */
  
  #include <common.h>
  #include <command.h>
  #include <exports.h>
  
  #include <nand.h>
  #include <onenand_uboot.h>
  #include <linux/mtd/mtd.h>
  #include <linux/mtd/partitions.h>
  #include <ubi_uboot.h>
  #include <asm/errno.h>
  #include <jffs2/load_kernel.h>
147162dac   Joe Hershberger   ubi: ubifs: Turn ...
25
26
27
  #undef ubi_msg
  #define ubi_msg(fmt, ...) printf("UBI: " fmt "
  ", ##__VA_ARGS__)
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
28
29
30
  #define DEV_TYPE_NONE		0
  #define DEV_TYPE_NAND		1
  #define DEV_TYPE_ONENAND	2
25ea652e9   Piotr Ziecik   UBI: Add proof-of...
31
  #define DEV_TYPE_NOR		3
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
32
33
34
  
  /* Private own data */
  static struct ubi_device *ubi;
a5c406701   Stefan Roese   UBI: Change parsi...
35
  static char buffer[80];
2ee951ba2   Stefan Roese   UBI: Enable re-in...
36
  static int ubi_initialized;
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
37
38
  
  struct selected_dev {
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
39
  	char part_name[80];
2d579e506   Stefan Roese   ubi: Remove flash...
40
  	int selected;
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
41
42
43
44
45
  	int nr;
  	struct mtd_info *mtd_info;
  };
  
  static struct selected_dev ubi_dev;
2f15cfd18   Stefan Roese   UBI/UBIFS: Automa...
46
47
48
49
  #ifdef CONFIG_CMD_UBIFS
  int ubifs_is_mounted(void);
  void cmd_ubifs_umount(void);
  #endif
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
  static void ubi_dump_vol_info(const struct ubi_volume *vol)
  {
  	ubi_msg("volume information dump:");
  	ubi_msg("vol_id          %d", vol->vol_id);
  	ubi_msg("reserved_pebs   %d", vol->reserved_pebs);
  	ubi_msg("alignment       %d", vol->alignment);
  	ubi_msg("data_pad        %d", vol->data_pad);
  	ubi_msg("vol_type        %d", vol->vol_type);
  	ubi_msg("name_len        %d", vol->name_len);
  	ubi_msg("usable_leb_size %d", vol->usable_leb_size);
  	ubi_msg("used_ebs        %d", vol->used_ebs);
  	ubi_msg("used_bytes      %lld", vol->used_bytes);
  	ubi_msg("last_eb_bytes   %d", vol->last_eb_bytes);
  	ubi_msg("corrupted       %d", vol->corrupted);
  	ubi_msg("upd_marker      %d", vol->upd_marker);
  
  	if (vol->name_len <= UBI_VOL_NAME_MAX &&
  		strnlen(vol->name, vol->name_len + 1) == vol->name_len) {
  		ubi_msg("name            %s", vol->name);
  	} else {
  		ubi_msg("the 1st 5 characters of the name: %c%c%c%c%c",
  				vol->name[0], vol->name[1], vol->name[2],
  				vol->name[3], vol->name[4]);
  	}
  	printf("
  ");
  }
  
  static void display_volume_info(struct ubi_device *ubi)
  {
  	int i;
  
  	for (i = 0; i < (ubi->vtbl_slots + 1); i++) {
  		if (!ubi->volumes[i])
  			continue;	/* Empty record */
  		ubi_dump_vol_info(ubi->volumes[i]);
  	}
  }
  
  static void display_ubi_info(struct ubi_device *ubi)
  {
  	ubi_msg("MTD device name:            \"%s\"", ubi->mtd->name);
  	ubi_msg("MTD device size:            %llu MiB", ubi->flash_size >> 20);
  	ubi_msg("physical eraseblock size:   %d bytes (%d KiB)",
  			ubi->peb_size, ubi->peb_size >> 10);
  	ubi_msg("logical eraseblock size:    %d bytes", ubi->leb_size);
  	ubi_msg("number of good PEBs:        %d", ubi->good_peb_count);
  	ubi_msg("number of bad PEBs:         %d", ubi->bad_peb_count);
  	ubi_msg("smallest flash I/O unit:    %d", ubi->min_io_size);
  	ubi_msg("VID header offset:          %d (aligned %d)",
  			ubi->vid_hdr_offset, ubi->vid_hdr_aloffset);
  	ubi_msg("data offset:                %d", ubi->leb_start);
  	ubi_msg("max. allowed volumes:       %d", ubi->vtbl_slots);
  	ubi_msg("wear-leveling threshold:    %d", CONFIG_MTD_UBI_WL_THRESHOLD);
  	ubi_msg("number of internal volumes: %d", UBI_INT_VOL_COUNT);
  	ubi_msg("number of user volumes:     %d",
  			ubi->vol_count - UBI_INT_VOL_COUNT);
  	ubi_msg("available PEBs:             %d", ubi->avail_pebs);
  	ubi_msg("total number of reserved PEBs: %d", ubi->rsvd_pebs);
  	ubi_msg("number of PEBs reserved for bad PEB handling: %d",
  			ubi->beb_rsvd_pebs);
  	ubi_msg("max/mean erase counter: %d/%d", ubi->max_ec, ubi->mean_ec);
  }
  
  static int ubi_info(int layout)
  {
  	if (layout)
  		display_volume_info(ubi);
  	else
  		display_ubi_info(ubi);
  
  	return 0;
  }
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
123
124
125
  static int verify_mkvol_req(const struct ubi_device *ubi,
  			    const struct ubi_mkvol_req *req)
  {
7f5d8a4d8   Stefan Roese   UBI: Fix error co...
126
  	int n, err = EINVAL;
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
127
128
129
130
131
132
133
134
135
136
137
  
  	if (req->bytes < 0 || req->alignment < 0 || req->vol_type < 0 ||
  	    req->name_len < 0)
  		goto bad;
  
  	if ((req->vol_id < 0 || req->vol_id >= ubi->vtbl_slots) &&
  	    req->vol_id != UBI_VOL_NUM_AUTO)
  		goto bad;
  
  	if (req->alignment == 0)
  		goto bad;
7f5d8a4d8   Stefan Roese   UBI: Fix error co...
138
139
140
141
  	if (req->bytes == 0) {
  		printf("No space left in UBI device!
  ");
  		err = ENOMEM;
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
142
  		goto bad;
7f5d8a4d8   Stefan Roese   UBI: Fix error co...
143
  	}
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
144
145
146
147
148
149
150
151
152
153
154
155
156
  
  	if (req->vol_type != UBI_DYNAMIC_VOLUME &&
  	    req->vol_type != UBI_STATIC_VOLUME)
  		goto bad;
  
  	if (req->alignment > ubi->leb_size)
  		goto bad;
  
  	n = req->alignment % ubi->min_io_size;
  	if (req->alignment != 1 && n)
  		goto bad;
  
  	if (req->name_len > UBI_VOL_NAME_MAX) {
7f5d8a4d8   Stefan Roese   UBI: Fix error co...
157
158
159
  		printf("Name too long!
  ");
  		err = ENAMETOOLONG;
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
160
161
162
163
164
  		goto bad;
  	}
  
  	return 0;
  bad:
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
165
166
  	return err;
  }
dd7185f17   Paul Burton   cmd_ubi: use int6...
167
  static int ubi_create_vol(char *volume, int64_t size, int dynamic)
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
  {
  	struct ubi_mkvol_req req;
  	int err;
  
  	if (dynamic)
  		req.vol_type = UBI_DYNAMIC_VOLUME;
  	else
  		req.vol_type = UBI_STATIC_VOLUME;
  
  	req.vol_id = UBI_VOL_NUM_AUTO;
  	req.alignment = 1;
  	req.bytes = size;
  
  	strcpy(req.name, volume);
  	req.name_len = strlen(volume);
  	req.name[req.name_len] = '\0';
  	req.padding1 = 0;
  	/* It's duplicated at drivers/mtd/ubi/cdev.c */
  	err = verify_mkvol_req(ubi, &req);
  	if (err) {
  		printf("verify_mkvol_req failed %d
  ", err);
  		return err;
  	}
dd7185f17   Paul Burton   cmd_ubi: use int6...
192
193
  	printf("Creating %s volume %s of size %lld
  ",
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
194
195
196
197
  		dynamic ? "dynamic" : "static", volume, size);
  	/* Call real ubi create volume */
  	return ubi_create_volume(ubi, &req);
  }
7f5d8a4d8   Stefan Roese   UBI: Fix error co...
198
  static struct ubi_volume *ubi_find_volume(char *volume)
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
199
  {
3b653fdb3   Peter Tyser   cmd_ubi: Fix unin...
200
  	struct ubi_volume *vol = NULL;
7f5d8a4d8   Stefan Roese   UBI: Fix error co...
201
  	int i;
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
202
203
204
  
  	for (i = 0; i < ubi->vtbl_slots; i++) {
  		vol = ubi->volumes[i];
7f5d8a4d8   Stefan Roese   UBI: Fix error co...
205
206
  		if (vol && !strcmp(vol->name, volume))
  			return vol;
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
207
  	}
7f5d8a4d8   Stefan Roese   UBI: Fix error co...
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
  
  	printf("Volume %s not found!
  ", volume);
  	return NULL;
  }
  
  static int ubi_remove_vol(char *volume)
  {
  	int err, reserved_pebs, i;
  	struct ubi_volume *vol;
  
  	vol = ubi_find_volume(volume);
  	if (vol == NULL)
  		return ENODEV;
  
  	printf("Remove UBI volume %s (id %d)
  ", vol->name, vol->vol_id);
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
225
226
227
228
  
  	if (ubi->ro_mode) {
  		printf("It's read-only mode
  ");
7f5d8a4d8   Stefan Roese   UBI: Fix error co...
229
  		err = EROFS;
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
230
231
  		goto out_err;
  	}
7f5d8a4d8   Stefan Roese   UBI: Fix error co...
232
  	err = ubi_change_vtbl_record(ubi, vol->vol_id, NULL);
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
233
234
235
236
237
238
239
240
241
242
243
244
245
  	if (err) {
  		printf("Error changing Vol tabel record err=%x
  ", err);
  		goto out_err;
  	}
  	reserved_pebs = vol->reserved_pebs;
  	for (i = 0; i < vol->reserved_pebs; i++) {
  		err = ubi_eba_unmap_leb(ubi, vol, i);
  		if (err)
  			goto out_err;
  	}
  
  	kfree(vol->eba_tbl);
7f5d8a4d8   Stefan Roese   UBI: Fix error co...
246
247
  	ubi->volumes[vol->vol_id]->eba_tbl = NULL;
  	ubi->volumes[vol->vol_id] = NULL;
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
  
  	ubi->rsvd_pebs -= reserved_pebs;
  	ubi->avail_pebs += reserved_pebs;
  	i = ubi->beb_rsvd_level - ubi->beb_rsvd_pebs;
  	if (i > 0) {
  		i = ubi->avail_pebs >= i ? i : ubi->avail_pebs;
  		ubi->avail_pebs -= i;
  		ubi->rsvd_pebs += i;
  		ubi->beb_rsvd_pebs += i;
  		if (i > 0)
  			ubi_msg("reserve more %d PEBs", i);
  	}
  	ubi->vol_count -= 1;
  
  	return 0;
  out_err:
7f5d8a4d8   Stefan Roese   UBI: Fix error co...
264
265
266
  	ubi_err("cannot remove volume %s, error %d", volume, err);
  	if (err < 0)
  		err = -err;
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
267
268
  	return err;
  }
cc734f5ab   Paul Burton   cmd_ubi: add writ...
269
  int ubi_volume_continue_write(char *volume, void *buf, size_t size)
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
270
  {
7f5d8a4d8   Stefan Roese   UBI: Fix error co...
271
  	int err = 1;
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
272
  	struct ubi_volume *vol;
7f5d8a4d8   Stefan Roese   UBI: Fix error co...
273
274
275
  	vol = ubi_find_volume(volume);
  	if (vol == NULL)
  		return ENODEV;
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
276
277
  	err = ubi_more_update_data(ubi, vol, buf, size);
  	if (err < 0) {
7f5d8a4d8   Stefan Roese   UBI: Fix error co...
278
279
280
  		printf("Couldnt or partially wrote data
  ");
  		return -err;
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
281
282
283
284
285
286
  	}
  
  	if (err) {
  		size = err;
  
  		err = ubi_check_volume(ubi, vol->vol_id);
7f5d8a4d8   Stefan Roese   UBI: Fix error co...
287
288
  		if (err < 0)
  			return -err;
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
289
290
291
292
293
294
295
296
297
298
299
300
301
  
  		if (err) {
  			ubi_warn("volume %d on UBI device %d is corrupted",
  					vol->vol_id, ubi->ubi_num);
  			vol->corrupted = 1;
  		}
  
  		vol->checked = 1;
  		ubi_gluebi_updated(vol);
  	}
  
  	return 0;
  }
cc734f5ab   Paul Burton   cmd_ubi: add writ...
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
  int ubi_volume_begin_write(char *volume, void *buf, size_t size,
  	size_t full_size)
  {
  	int err = 1;
  	int rsvd_bytes = 0;
  	struct ubi_volume *vol;
  
  	vol = ubi_find_volume(volume);
  	if (vol == NULL)
  		return ENODEV;
  
  	rsvd_bytes = vol->reserved_pebs * (ubi->leb_size - vol->data_pad);
  	if (size < 0 || size > rsvd_bytes) {
  		printf("size > volume size! Aborting!
  ");
  		return EINVAL;
  	}
  
  	err = ubi_start_update(ubi, vol, full_size);
  	if (err < 0) {
  		printf("Cannot start volume update
  ");
  		return -err;
  	}
  
  	return ubi_volume_continue_write(volume, buf, size);
  }
  
  int ubi_volume_write(char *volume, void *buf, size_t size)
  {
  	return ubi_volume_begin_write(volume, buf, size, size);
  }
718290675   Joe Hershberger   ubi: Expose a few...
334
  int ubi_volume_read(char *volume, char *buf, size_t size)
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
335
  {
7f5d8a4d8   Stefan Roese   UBI: Fix error co...
336
  	int err, lnum, off, len, tbuf_size;
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
337
338
  	void *tbuf;
  	unsigned long long tmp;
7f5d8a4d8   Stefan Roese   UBI: Fix error co...
339
  	struct ubi_volume *vol;
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
340
  	loff_t offp = 0;
7f5d8a4d8   Stefan Roese   UBI: Fix error co...
341
342
343
  	vol = ubi_find_volume(volume);
  	if (vol == NULL)
  		return ENODEV;
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
344

694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
345
346
  	if (vol->updating) {
  		printf("updating");
7f5d8a4d8   Stefan Roese   UBI: Fix error co...
347
  		return EBUSY;
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
348
349
350
  	}
  	if (vol->upd_marker) {
  		printf("damaged volume, update marker is set");
7f5d8a4d8   Stefan Roese   UBI: Fix error co...
351
  		return EBADF;
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
352
353
354
355
356
  	}
  	if (offp == vol->used_bytes)
  		return 0;
  
  	if (size == 0) {
7f5d8a4d8   Stefan Roese   UBI: Fix error co...
357
358
  		printf("No size specified -> Using max size (%lld)
  ", vol->used_bytes);
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
359
360
361
362
363
364
  		size = vol->used_bytes;
  	}
  
  	if (vol->corrupted)
  		printf("read from corrupted volume %d", vol->vol_id);
  	if (offp + size > vol->used_bytes)
2984fd167   Marek Vasut   GCC4.6: Squash wa...
365
  		size = vol->used_bytes - offp;
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
366
367
368
369
370
371
372
373
  
  	tbuf_size = vol->usable_leb_size;
  	if (size < tbuf_size)
  		tbuf_size = ALIGN(size, ubi->min_io_size);
  	tbuf = malloc(tbuf_size);
  	if (!tbuf) {
  		printf("NO MEM
  ");
7f5d8a4d8   Stefan Roese   UBI: Fix error co...
374
  		return ENOMEM;
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
375
376
377
378
379
380
  	}
  	len = size > tbuf_size ? tbuf_size : size;
  
  	tmp = offp;
  	off = do_div(tmp, vol->usable_leb_size);
  	lnum = tmp;
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
381
382
383
384
385
386
387
388
  	do {
  		if (off + len >= vol->usable_leb_size)
  			len = vol->usable_leb_size - off;
  
  		err = ubi_eba_read_leb(ubi, vol, lnum, tbuf, off, len, 0);
  		if (err) {
  			printf("read err %x
  ", err);
7f5d8a4d8   Stefan Roese   UBI: Fix error co...
389
  			err = -err;
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
390
391
392
393
394
395
396
397
398
399
  			break;
  		}
  		off += len;
  		if (off == vol->usable_leb_size) {
  			lnum += 1;
  			off -= vol->usable_leb_size;
  		}
  
  		size -= len;
  		offp += len;
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
400
  		memcpy(buf, tbuf, len);
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
401
402
403
404
405
406
  
  		buf += len;
  		len = size > tbuf_size ? tbuf_size : size;
  	} while (size);
  
  	free(tbuf);
7f5d8a4d8   Stefan Roese   UBI: Fix error co...
407
  	return err;
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
408
  }
25c8f4005   Simon Kagstrom   Handle VID header...
409
410
  static int ubi_dev_scan(struct mtd_info *info, char *ubidev,
  		const char *vid_header_offset)
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
411
412
413
414
  {
  	struct mtd_device *dev;
  	struct part_info *part;
  	struct mtd_partition mtd_part;
25c8f4005   Simon Kagstrom   Handle VID header...
415
  	char ubi_mtd_param_buffer[80];
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
416
417
  	u8 pnum;
  	int err;
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
418
419
420
421
422
423
424
425
426
  	if (find_dev_and_part(ubidev, &dev, &pnum, &part) != 0)
  		return 1;
  
  	sprintf(buffer, "mtd=%d", pnum);
  	memset(&mtd_part, 0, sizeof(mtd_part));
  	mtd_part.name = buffer;
  	mtd_part.size = part->size;
  	mtd_part.offset = part->offset;
  	add_mtd_partitions(info, &mtd_part, 1);
25c8f4005   Simon Kagstrom   Handle VID header...
427
428
429
430
431
  	strcpy(ubi_mtd_param_buffer, buffer);
  	if (vid_header_offset)
  		sprintf(ubi_mtd_param_buffer, "mtd=%d,%s", pnum,
  				vid_header_offset);
  	err = ubi_mtd_param_parse(ubi_mtd_param_buffer, NULL);
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
432
433
  	if (err) {
  		del_mtd_partitions(info);
7f5d8a4d8   Stefan Roese   UBI: Fix error co...
434
  		return -err;
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
435
436
437
438
439
  	}
  
  	err = ubi_init();
  	if (err) {
  		del_mtd_partitions(info);
7f5d8a4d8   Stefan Roese   UBI: Fix error co...
440
  		return -err;
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
441
  	}
2ee951ba2   Stefan Roese   UBI: Enable re-in...
442
  	ubi_initialized = 1;
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
443
444
  	return 0;
  }
718290675   Joe Hershberger   ubi: Expose a few...
445
  int ubi_part(char *part_name, const char *vid_header_offset)
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
446
  {
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
447
  	int err = 0;
718290675   Joe Hershberger   ubi: Expose a few...
448
449
450
451
  	char mtd_dev[16];
  	struct mtd_device *dev;
  	struct part_info *part;
  	u8 pnum;
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
452

c203ef5db   Andreas Huber   UBI/cfi-mtd: Fix ...
453
454
455
456
457
  	if (mtdparts_init() != 0) {
  		printf("Error initializing mtdparts!
  ");
  		return 1;
  	}
718290675   Joe Hershberger   ubi: Expose a few...
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
  #ifdef CONFIG_CMD_UBIFS
  	/*
  	 * Automatically unmount UBIFS partition when user
  	 * changes the UBI device. Otherwise the following
  	 * UBIFS commands will crash.
  	 */
  	if (ubifs_is_mounted())
  		cmd_ubifs_umount();
  #endif
  
  	/* todo: get dev number for NAND... */
  	ubi_dev.nr = 0;
  
  	/*
  	 * Call ubi_exit() before re-initializing the UBI subsystem
  	 */
  	if (ubi_initialized) {
  		ubi_exit();
  		del_mtd_partitions(ubi_dev.mtd_info);
  	}
  
  	/*
  	 * Search the mtd device number where this partition
  	 * is located
  	 */
  	if (find_dev_and_part(part_name, &dev, &pnum, &part)) {
  		printf("Partition %s not found!
  ", part_name);
  		return 1;
  	}
  	sprintf(mtd_dev, "%s%d", MTD_DEV_TYPE(dev->id->type), dev->id->num);
  	ubi_dev.mtd_info = get_mtd_device_nm(mtd_dev);
  	if (IS_ERR(ubi_dev.mtd_info)) {
  		printf("Partition %s not found on device %s!
  ", part_name,
  		       mtd_dev);
  		return 1;
  	}
  
  	ubi_dev.selected = 1;
  
  	strcpy(ubi_dev.part_name, part_name);
  	err = ubi_dev_scan(ubi_dev.mtd_info, ubi_dev.part_name,
  			vid_header_offset);
  	if (err) {
  		printf("UBI init error %d
  ", err);
  		ubi_dev.selected = 0;
  		return err;
  	}
  
  	ubi = ubi_devices[0];
  
  	return 0;
  }
  
  static int do_ubi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  {
dd7185f17   Paul Burton   cmd_ubi: use int6...
516
  	int64_t size = 0;
718290675   Joe Hershberger   ubi: Expose a few...
517
518
519
520
  	ulong addr = 0;
  
  	if (argc < 2)
  		return CMD_RET_USAGE;
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
521
  	if (strcmp(argv[1], "part") == 0) {
25c8f4005   Simon Kagstrom   Handle VID header...
522
  		const char *vid_header_offset = NULL;
2d579e506   Stefan Roese   ubi: Remove flash...
523

694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
524
525
  		/* Print current partition */
  		if (argc == 2) {
2d579e506   Stefan Roese   ubi: Remove flash...
526
  			if (!ubi_dev.selected) {
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
527
528
529
530
  				printf("Error, no UBI device/partition selected!
  ");
  				return 1;
  			}
2d579e506   Stefan Roese   ubi: Remove flash...
531
532
  			printf("Device %d: %s, partition %s
  ",
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
533
534
535
  			       ubi_dev.nr, ubi_dev.mtd_info->name, ubi_dev.part_name);
  			return 0;
  		}
47e26b1bf   Wolfgang Denk   cmd_usage(): simp...
536
  		if (argc < 3)
4c12eeb8b   Simon Glass   Convert cmd_usage...
537
  			return CMD_RET_USAGE;
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
538

25c8f4005   Simon Kagstrom   Handle VID header...
539
540
  		if (argc > 3)
  			vid_header_offset = argv[3];
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
541

718290675   Joe Hershberger   ubi: Expose a few...
542
  		return ubi_part(argv[2], vid_header_offset);
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
543
  	}
2d579e506   Stefan Roese   ubi: Remove flash...
544
  	if ((strcmp(argv[1], "part") != 0) && (!ubi_dev.selected)) {
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
  		printf("Error, no UBI device/partition selected!
  ");
  		return 1;
  	}
  
  	if (strcmp(argv[1], "info") == 0) {
  		int layout = 0;
  		if (argc > 2 && !strncmp(argv[2], "l", 1))
  			layout = 1;
  		return ubi_info(layout);
  	}
  
  	if (strncmp(argv[1], "create", 6) == 0) {
  		int dynamic = 1;	/* default: dynamic volume */
  
  		/* Use maximum available size */
  		size = 0;
  
  		/* E.g., create volume size type */
  		if (argc == 5) {
  			if (strncmp(argv[4], "s", 1) == 0)
  				dynamic = 0;
  			else if (strncmp(argv[4], "d", 1) != 0) {
  				printf("Incorrect type
  ");
  				return 1;
  			}
  			argc--;
  		}
  		/* E.g., create volume size */
  		if (argc == 4) {
dd7185f17   Paul Burton   cmd_ubi: use int6...
576
  			size = simple_strtoull(argv[3], NULL, 16);
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
577
578
579
  			argc--;
  		}
  		/* Use maximum available size */
7f5d8a4d8   Stefan Roese   UBI: Fix error co...
580
  		if (!size) {
dd7185f17   Paul Burton   cmd_ubi: use int6...
581
582
583
  			size = (int64_t)ubi->avail_pebs * ubi->leb_size;
  			printf("No size specified -> Using max size (%lld)
  ", size);
7f5d8a4d8   Stefan Roese   UBI: Fix error co...
584
  		}
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
585
586
587
588
589
590
591
592
593
594
595
596
  		/* E.g., create volume */
  		if (argc == 3)
  			return ubi_create_vol(argv[2], size, dynamic);
  	}
  
  	if (strncmp(argv[1], "remove", 6) == 0) {
  		/* E.g., remove volume */
  		if (argc == 3)
  			return ubi_remove_vol(argv[2]);
  	}
  
  	if (strncmp(argv[1], "write", 5) == 0) {
718290675   Joe Hershberger   ubi: Expose a few...
597
  		int ret;
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
598
599
600
601
602
603
604
  		if (argc < 5) {
  			printf("Please see usage
  ");
  			return 1;
  		}
  
  		addr = simple_strtoul(argv[2], NULL, 16);
a5c406701   Stefan Roese   UBI: Change parsi...
605
  		size = simple_strtoul(argv[4], NULL, 16);
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
606

cc734f5ab   Paul Burton   cmd_ubi: add writ...
607
608
609
610
611
612
613
614
615
616
617
618
619
620
  		if (strlen(argv[1]) == 10 &&
  		    strncmp(argv[1] + 5, ".part", 5) == 0) {
  			if (argc < 6) {
  				ret = ubi_volume_continue_write(argv[3],
  						(void *)addr, size);
  			} else {
  				size_t full_size;
  				full_size = simple_strtoul(argv[5], NULL, 16);
  				ret = ubi_volume_begin_write(argv[3],
  						(void *)addr, size, full_size);
  			}
  		} else {
  			ret = ubi_volume_write(argv[3], (void *)addr, size);
  		}
718290675   Joe Hershberger   ubi: Expose a few...
621
  		if (!ret) {
dd7185f17   Paul Burton   cmd_ubi: use int6...
622
623
  			printf("%lld bytes written to volume %s
  ", size,
718290675   Joe Hershberger   ubi: Expose a few...
624
625
626
627
  			       argv[3]);
  		}
  
  		return ret;
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
628
629
630
631
632
633
634
  	}
  
  	if (strncmp(argv[1], "read", 4) == 0) {
  		size = 0;
  
  		/* E.g., read volume size */
  		if (argc == 5) {
a5c406701   Stefan Roese   UBI: Change parsi...
635
  			size = simple_strtoul(argv[4], NULL, 16);
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
636
637
638
639
640
641
642
643
  			argc--;
  		}
  
  		/* E.g., read volume */
  		if (argc == 4) {
  			addr = simple_strtoul(argv[2], NULL, 16);
  			argc--;
  		}
718290675   Joe Hershberger   ubi: Expose a few...
644
  		if (argc == 3) {
dd7185f17   Paul Burton   cmd_ubi: use int6...
645
646
  			printf("Read %lld bytes from volume %s to %lx
  ", size,
718290675   Joe Hershberger   ubi: Expose a few...
647
  			       argv[3], addr);
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
648
  			return ubi_volume_read(argv[3], (char *)addr, size);
718290675   Joe Hershberger   ubi: Expose a few...
649
  		}
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
650
651
652
653
  	}
  
  	printf("Please see usage
  ");
7f5d8a4d8   Stefan Roese   UBI: Fix error co...
654
  	return 1;
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
655
  }
388a29d02   Frans Meulenbroeks   various cmd_* fil...
656
657
  U_BOOT_CMD(
  	ubi, 6, 1, do_ubi,
2fb2604d5   Peter Tyser   Command usage cle...
658
  	"ubi commands",
25c8f4005   Simon Kagstrom   Handle VID header...
659
660
661
662
663
  	"part [part] [offset]
  "
  		" - Show or set current partition (with optional VID"
  		" header offset)
  "
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
664
665
666
667
668
669
670
671
672
  	"ubi info [l[ayout]]"
  		" - Display volume and ubi layout information
  "
  	"ubi create[vol] volume [size] [type]"
  		" - create volume name with size
  "
  	"ubi write[vol] address volume size"
  		" - Write volume from address with size
  "
cc734f5ab   Paul Burton   cmd_ubi: add writ...
673
674
675
676
  	"ubi write.part address volume size [fullsize]
  "
  		" - Write part of a volume from address
  "
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
677
678
679
680
681
682
683
684
  	"ubi read[vol] address volume [size]"
  		" - Read volume to address with size
  "
  	"ubi remove[vol] volume"
  		" - Remove volume
  "
  	"[Legends]
  "
f6ca3b709   Andrzej Wolski   ubi: help message...
685
686
687
688
  	" volume: character name
  "
  	" size: specified in bytes
  "
a89c33db9   Wolfgang Denk   General help mess...
689
  	" type: s[tatic] or d[ynamic] (default=dynamic)"
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
690
  );