Blame view

cmd/ubi.c 16.2 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
   *
   * 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>
c7694dd48   Simon Glass   env: Move env_set...
16
  #include <env.h>
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
17
  #include <exports.h>
336d4615f   Simon Glass   dm: core: Create ...
18
  #include <malloc.h>
6e295186c   Simon Glass   Move malloc_cache...
19
  #include <memalign.h>
c58fb2cdb   Miquel Raynal   cmd: ubi: clean t...
20
  #include <mtd.h>
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
21
22
  #include <nand.h>
  #include <onenand_uboot.h>
61b29b826   Simon Glass   dm: core: Require...
23
  #include <dm/devres.h>
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
24
25
  #include <linux/mtd/mtd.h>
  #include <linux/mtd/partitions.h>
ff94bc40a   Heiko Schocher   mtd, ubi, ubifs: ...
26
  #include <linux/err.h>
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
27
  #include <ubi_uboot.h>
1221ce459   Masahiro Yamada   treewide: replace...
28
  #include <linux/errno.h>
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
29
  #include <jffs2/load_kernel.h>
147162dac   Joe Hershberger   ubi: ubifs: Turn ...
30
31
32
  #undef ubi_msg
  #define ubi_msg(fmt, ...) printf("UBI: " fmt "
  ", ##__VA_ARGS__)
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
33
34
  /* Private own data */
  static struct ubi_device *ubi;
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
35

2f15cfd18   Stefan Roese   UBI/UBIFS: Automa...
36
  #ifdef CONFIG_CMD_UBIFS
10c204406   Tien Fong Chee   cmd: ubifs: Move ...
37
  #include <ubifs_uboot.h>
2f15cfd18   Stefan Roese   UBI/UBIFS: Automa...
38
  #endif
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
39
40
41
42
43
44
45
46
47
48
49
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
  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;
  }
f9f4d809a   Heiko Schocher   common, ubi: add ...
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
  static int ubi_check_volumename(const struct ubi_volume *vol, char *name)
  {
  	return strcmp(vol->name, name);
  }
  
  static int ubi_check(char *name)
  {
  	int i;
  
  	for (i = 0; i < (ubi->vtbl_slots + 1); i++) {
  		if (!ubi->volumes[i])
  			continue;	/* Empty record */
  
  		if (!ubi_check_volumename(ubi->volumes[i], name))
  			return 0;
  	}
6d0f45260   Stefan Agner   common, ubi: use ...
100
  	return 1;
f9f4d809a   Heiko Schocher   common, ubi: add ...
101
  }
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
102
103
104
  static int verify_mkvol_req(const struct ubi_device *ubi,
  			    const struct ubi_mkvol_req *req)
  {
7f5d8a4d8   Stefan Roese   UBI: Fix error co...
105
  	int n, err = EINVAL;
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
106
107
108
109
110
111
112
113
114
115
116
  
  	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...
117
118
119
120
  	if (req->bytes == 0) {
  		printf("No space left in UBI device!
  ");
  		err = ENOMEM;
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
121
  		goto bad;
7f5d8a4d8   Stefan Roese   UBI: Fix error co...
122
  	}
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
123
124
125
126
127
128
129
130
131
132
133
134
135
  
  	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...
136
137
138
  		printf("Name too long!
  ");
  		err = ENAMETOOLONG;
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
139
140
141
142
143
  		goto bad;
  	}
  
  	return 0;
  bad:
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
144
145
  	return err;
  }
386f20cad   Quentin Schulz   ubi: provide a wa...
146
147
  static int ubi_create_vol(char *volume, int64_t size, int dynamic, int vol_id,
  			  bool skipcheck)
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
148
149
150
151
152
153
154
155
  {
  	struct ubi_mkvol_req req;
  	int err;
  
  	if (dynamic)
  		req.vol_type = UBI_DYNAMIC_VOLUME;
  	else
  		req.vol_type = UBI_STATIC_VOLUME;
006124223   Ladislav Michl   cmd: ubi: add opt...
156
  	req.vol_id = vol_id;
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
157
158
159
160
161
162
  	req.alignment = 1;
  	req.bytes = size;
  
  	strcpy(req.name, volume);
  	req.name_len = strlen(volume);
  	req.name[req.name_len] = '\0';
386f20cad   Quentin Schulz   ubi: provide a wa...
163
164
165
  	req.flags = 0;
  	if (skipcheck)
  		req.flags |= UBI_VOL_SKIP_CRC_CHECK_FLG;
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
166
167
168
169
170
171
172
  	/* 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...
173
174
  	printf("Creating %s volume %s of size %lld
  ",
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
175
176
177
178
  		dynamic ? "dynamic" : "static", volume, size);
  	/* Call real ubi create volume */
  	return ubi_create_volume(ubi, &req);
  }
7f5d8a4d8   Stefan Roese   UBI: Fix error co...
179
  static struct ubi_volume *ubi_find_volume(char *volume)
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
180
  {
3b653fdb3   Peter Tyser   cmd_ubi: Fix unin...
181
  	struct ubi_volume *vol = NULL;
7f5d8a4d8   Stefan Roese   UBI: Fix error co...
182
  	int i;
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
183
184
185
  
  	for (i = 0; i < ubi->vtbl_slots; i++) {
  		vol = ubi->volumes[i];
7f5d8a4d8   Stefan Roese   UBI: Fix error co...
186
187
  		if (vol && !strcmp(vol->name, volume))
  			return vol;
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
188
  	}
7f5d8a4d8   Stefan Roese   UBI: Fix error co...
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
  
  	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...
206
207
208
209
  
  	if (ubi->ro_mode) {
  		printf("It's read-only mode
  ");
7f5d8a4d8   Stefan Roese   UBI: Fix error co...
210
  		err = EROFS;
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
211
212
  		goto out_err;
  	}
7f5d8a4d8   Stefan Roese   UBI: Fix error co...
213
  	err = ubi_change_vtbl_record(ubi, vol->vol_id, NULL);
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
214
215
216
217
218
219
220
221
222
223
224
225
226
  	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...
227
228
  	ubi->volumes[vol->vol_id]->eba_tbl = NULL;
  	ubi->volumes[vol->vol_id] = NULL;
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
  
  	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:
0195a7bb3   Heiko Schocher   ubi,ubifs: sync w...
245
  	ubi_err(ubi, "cannot remove volume %s, error %d", volume, err);
7f5d8a4d8   Stefan Roese   UBI: Fix error co...
246
247
  	if (err < 0)
  		err = -err;
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
248
249
  	return err;
  }
83f7078b6   Philippe Reynes   cmd: ubi: add a c...
250
251
252
253
254
255
256
257
258
259
260
261
262
  static int ubi_rename_vol(char *oldname, char *newname)
  {
  	struct ubi_volume *vol;
  	struct ubi_rename_entry rename;
  	struct ubi_volume_desc desc;
  	struct list_head list;
  
  	vol = ubi_find_volume(oldname);
  	if (!vol) {
  		printf("%s: volume %s doesn't exist
  ", __func__, oldname);
  		return ENODEV;
  	}
1639988fa   Philippe Reynes   cmd: ubi: don't a...
263
264
265
266
267
  	if (!ubi_check(newname)) {
  		printf("%s: volume %s already exist
  ", __func__, newname);
  		return EINVAL;
  	}
83f7078b6   Philippe Reynes   cmd: ubi: add a c...
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
  	printf("Rename UBI volume %s to %s
  ", oldname, newname);
  
  	if (ubi->ro_mode) {
  		printf("%s: ubi device is in read-only mode
  ", __func__);
  		return EROFS;
  	}
  
  	rename.new_name_len = strlen(newname);
  	strcpy(rename.new_name, newname);
  	rename.remove = 0;
  	desc.vol = vol;
  	desc.mode = 0;
  	rename.desc = &desc;
  	INIT_LIST_HEAD(&rename.list);
  	INIT_LIST_HEAD(&list);
  	list_add(&rename.list, &list);
  
  	return ubi_rename_volumes(ubi, &list);
  }
0e350f81e   Jeroen Hofstee   common: commands:...
289
  static int ubi_volume_continue_write(char *volume, void *buf, size_t size)
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
290
  {
7f5d8a4d8   Stefan Roese   UBI: Fix error co...
291
  	int err = 1;
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
292
  	struct ubi_volume *vol;
7f5d8a4d8   Stefan Roese   UBI: Fix error co...
293
294
295
  	vol = ubi_find_volume(volume);
  	if (vol == NULL)
  		return ENODEV;
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
296
297
  	err = ubi_more_update_data(ubi, vol, buf, size);
  	if (err < 0) {
7f5d8a4d8   Stefan Roese   UBI: Fix error co...
298
299
300
  		printf("Couldnt or partially wrote data
  ");
  		return -err;
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
301
302
303
304
305
306
  	}
  
  	if (err) {
  		size = err;
  
  		err = ubi_check_volume(ubi, vol->vol_id);
7f5d8a4d8   Stefan Roese   UBI: Fix error co...
307
308
  		if (err < 0)
  			return -err;
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
309
310
  
  		if (err) {
0195a7bb3   Heiko Schocher   ubi,ubifs: sync w...
311
312
  			ubi_warn(ubi, "volume %d on UBI device %d is corrupt",
  				 vol->vol_id, ubi->ubi_num);
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
313
314
315
316
317
318
319
320
321
  			vol->corrupted = 1;
  		}
  
  		vol->checked = 1;
  		ubi_gluebi_updated(vol);
  	}
  
  	return 0;
  }
cc734f5ab   Paul Burton   cmd_ubi: add writ...
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);
18f41f2fa   xypron.glpk@gmx.de   cmd: ubi: remove ...
334
  	if (size > rsvd_bytes) {
cc734f5ab   Paul Burton   cmd_ubi: add writ...
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
  		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...
354
  int ubi_volume_read(char *volume, char *buf, size_t size)
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
355
  {
7f5d8a4d8   Stefan Roese   UBI: Fix error co...
356
  	int err, lnum, off, len, tbuf_size;
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
357
358
  	void *tbuf;
  	unsigned long long tmp;
7f5d8a4d8   Stefan Roese   UBI: Fix error co...
359
  	struct ubi_volume *vol;
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
360
  	loff_t offp = 0;
985fa93e6   Holger Dengler   cmd: set filesize...
361
  	size_t len_read;
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
362

7f5d8a4d8   Stefan Roese   UBI: Fix error co...
363
364
365
  	vol = ubi_find_volume(volume);
  	if (vol == NULL)
  		return ENODEV;
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
366

694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
367
368
  	if (vol->updating) {
  		printf("updating");
7f5d8a4d8   Stefan Roese   UBI: Fix error co...
369
  		return EBUSY;
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
370
371
372
  	}
  	if (vol->upd_marker) {
  		printf("damaged volume, update marker is set");
7f5d8a4d8   Stefan Roese   UBI: Fix error co...
373
  		return EBADF;
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
374
375
376
377
378
  	}
  	if (offp == vol->used_bytes)
  		return 0;
  
  	if (size == 0) {
7f5d8a4d8   Stefan Roese   UBI: Fix error co...
379
380
  		printf("No size specified -> Using max size (%lld)
  ", vol->used_bytes);
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
381
382
  		size = vol->used_bytes;
  	}
134153377   Tom Rini   cmd/ubi.c: Fix fo...
383
384
  	printf("Read %zu bytes from volume %s to %p
  ", size, volume, buf);
68c7025d9   Stefan Agner   cmd: ubi: print l...
385

694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
386
387
388
  	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...
389
  		size = vol->used_bytes - offp;
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
390
391
392
393
  
  	tbuf_size = vol->usable_leb_size;
  	if (size < tbuf_size)
  		tbuf_size = ALIGN(size, ubi->min_io_size);
4519668b2   Marcel Ziswiler   mtd/nand/ubi: ass...
394
  	tbuf = malloc_cache_aligned(tbuf_size);
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
395
396
397
  	if (!tbuf) {
  		printf("NO MEM
  ");
7f5d8a4d8   Stefan Roese   UBI: Fix error co...
398
  		return ENOMEM;
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
399
400
401
402
403
404
  	}
  	len = size > tbuf_size ? tbuf_size : size;
  
  	tmp = offp;
  	off = do_div(tmp, vol->usable_leb_size);
  	lnum = tmp;
985fa93e6   Holger Dengler   cmd: set filesize...
405
  	len_read = size;
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
406
407
408
409
410
411
412
413
  	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...
414
  			err = -err;
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
415
416
417
418
419
420
421
422
423
424
  			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...
425
  		memcpy(buf, tbuf, len);
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
426
427
428
429
  
  		buf += len;
  		len = size > tbuf_size ? tbuf_size : size;
  	} while (size);
985fa93e6   Holger Dengler   cmd: set filesize...
430
431
  	if (!size)
  		env_set_hex("filesize", len_read);
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
432
  	free(tbuf);
7f5d8a4d8   Stefan Roese   UBI: Fix error co...
433
  	return err;
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
434
  }
c58fb2cdb   Miquel Raynal   cmd: ubi: clean t...
435
  static int ubi_dev_scan(struct mtd_info *info, const char *vid_header_offset)
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
436
  {
25c8f4005   Simon Kagstrom   Handle VID header...
437
  	char ubi_mtd_param_buffer[80];
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
438
  	int err;
c58fb2cdb   Miquel Raynal   cmd: ubi: clean t...
439
440
441
442
443
  	if (!vid_header_offset)
  		sprintf(ubi_mtd_param_buffer, "%s", info->name);
  	else
  		sprintf(ubi_mtd_param_buffer, "%s,%s", info->name,
  			vid_header_offset);
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
444

25c8f4005   Simon Kagstrom   Handle VID header...
445
  	err = ubi_mtd_param_parse(ubi_mtd_param_buffer, NULL);
c58fb2cdb   Miquel Raynal   cmd: ubi: clean t...
446
  	if (err)
7f5d8a4d8   Stefan Roese   UBI: Fix error co...
447
  		return -err;
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
448
449
  
  	err = ubi_init();
c58fb2cdb   Miquel Raynal   cmd: ubi: clean t...
450
  	if (err)
7f5d8a4d8   Stefan Roese   UBI: Fix error co...
451
  		return -err;
2ee951ba2   Stefan Roese   UBI: Enable re-in...
452

694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
453
454
  	return 0;
  }
e6661cf76   Stefan Roese   ubi: Add "skipche...
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
  static int ubi_set_skip_check(char *volume, bool skip_check)
  {
  	struct ubi_vtbl_record vtbl_rec;
  	struct ubi_volume *vol;
  
  	vol = ubi_find_volume(volume);
  	if (!vol)
  		return ENODEV;
  
  	printf("%sing skip_check on volume %s
  ",
  	       skip_check ? "Sett" : "Clear", volume);
  
  	vtbl_rec = ubi->vtbl[vol->vol_id];
  	if (skip_check) {
  		vtbl_rec.flags |= UBI_VTBL_SKIP_CRC_CHECK_FLG;
  		vol->skip_check = 1;
  	} else {
  		vtbl_rec.flags &= ~UBI_VTBL_SKIP_CRC_CHECK_FLG;
  		vol->skip_check = 0;
  	}
  
  	return ubi_change_vtbl_record(ubi, vol->vol_id, &vtbl_rec);
  }
d821e5edf   Stefan Roese   cmd: ubi: Make ub...
479
  static int ubi_detach(void)
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
480
  {
718290675   Joe Hershberger   ubi: Expose a few...
481
482
483
484
485
486
487
488
489
  #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
718290675   Joe Hershberger   ubi: Expose a few...
490
491
492
  	/*
  	 * Call ubi_exit() before re-initializing the UBI subsystem
  	 */
c58fb2cdb   Miquel Raynal   cmd: ubi: clean t...
493
  	if (ubi)
718290675   Joe Hershberger   ubi: Expose a few...
494
  		ubi_exit();
718290675   Joe Hershberger   ubi: Expose a few...
495

c58fb2cdb   Miquel Raynal   cmd: ubi: clean t...
496
  	ubi = NULL;
cddfc97d1   Heiko Schocher   ubi: add new ubi ...
497
498
499
500
501
  	return 0;
  }
  
  int ubi_part(char *part_name, const char *vid_header_offset)
  {
c58fb2cdb   Miquel Raynal   cmd: ubi: clean t...
502
  	struct mtd_info *mtd;
cddfc97d1   Heiko Schocher   ubi: add new ubi ...
503
  	int err = 0;
cddfc97d1   Heiko Schocher   ubi: add new ubi ...
504
505
  
  	ubi_detach();
c58fb2cdb   Miquel Raynal   cmd: ubi: clean t...
506
507
508
509
  
  	mtd_probe_devices();
  	mtd = get_mtd_device_nm(part_name);
  	if (IS_ERR(mtd)) {
718290675   Joe Hershberger   ubi: Expose a few...
510
511
512
513
  		printf("Partition %s not found!
  ", part_name);
  		return 1;
  	}
c58fb2cdb   Miquel Raynal   cmd: ubi: clean t...
514
  	put_mtd_device(mtd);
718290675   Joe Hershberger   ubi: Expose a few...
515

c58fb2cdb   Miquel Raynal   cmd: ubi: clean t...
516
  	err = ubi_dev_scan(mtd, vid_header_offset);
718290675   Joe Hershberger   ubi: Expose a few...
517
518
519
  	if (err) {
  		printf("UBI init error %d
  ", err);
4a94e53b2   Stefan Roese   cmd: ubi: Add add...
520
521
  		printf("Please check, if the correct MTD partition is used (size big enough?)
  ");
718290675   Joe Hershberger   ubi: Expose a few...
522
523
524
525
526
527
528
  		return err;
  	}
  
  	ubi = ubi_devices[0];
  
  	return 0;
  }
091401131   Simon Glass   command: Remove t...
529
  static int do_ubi(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
718290675   Joe Hershberger   ubi: Expose a few...
530
  {
dd7185f17   Paul Burton   cmd_ubi: use int6...
531
  	int64_t size = 0;
718290675   Joe Hershberger   ubi: Expose a few...
532
  	ulong addr = 0;
386f20cad   Quentin Schulz   ubi: provide a wa...
533
  	bool skipcheck = false;
718290675   Joe Hershberger   ubi: Expose a few...
534
535
536
  
  	if (argc < 2)
  		return CMD_RET_USAGE;
76428561b   Heinrich Schuchardt   cmd: ubi: remove ...
537
  	if (strcmp(argv[1], "detach") == 0)
cddfc97d1   Heiko Schocher   ubi: add new ubi ...
538
  		return ubi_detach();
cddfc97d1   Heiko Schocher   ubi: add new ubi ...
539

694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
540
  	if (strcmp(argv[1], "part") == 0) {
25c8f4005   Simon Kagstrom   Handle VID header...
541
  		const char *vid_header_offset = NULL;
2d579e506   Stefan Roese   ubi: Remove flash...
542

694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
543
544
  		/* Print current partition */
  		if (argc == 2) {
c58fb2cdb   Miquel Raynal   cmd: ubi: clean t...
545
546
547
  			if (!ubi) {
  				printf("Error, no UBI device selected!
  ");
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
548
549
  				return 1;
  			}
c58fb2cdb   Miquel Raynal   cmd: ubi: clean t...
550
551
552
  			printf("Device %d: %s, MTD partition %s
  ",
  			       ubi->ubi_num, ubi->ubi_name, ubi->mtd->name);
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
553
554
  			return 0;
  		}
47e26b1bf   Wolfgang Denk   cmd_usage(): simp...
555
  		if (argc < 3)
4c12eeb8b   Simon Glass   Convert cmd_usage...
556
  			return CMD_RET_USAGE;
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
557

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

718290675   Joe Hershberger   ubi: Expose a few...
561
  		return ubi_part(argv[2], vid_header_offset);
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
562
  	}
c58fb2cdb   Miquel Raynal   cmd: ubi: clean t...
563
564
565
  	if ((strcmp(argv[1], "part") != 0) && !ubi) {
  		printf("Error, no UBI device selected!
  ");
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
566
567
568
569
570
571
572
573
574
  		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);
  	}
f9f4d809a   Heiko Schocher   common, ubi: add ...
575
576
577
578
579
580
581
582
  	if (strcmp(argv[1], "check") == 0) {
  		if (argc > 2)
  			return ubi_check(argv[2]);
  
  		printf("Error, no volume name passed
  ");
  		return 1;
  	}
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
583
584
  	if (strncmp(argv[1], "create", 6) == 0) {
  		int dynamic = 1;	/* default: dynamic volume */
006124223   Ladislav Michl   cmd: ubi: add opt...
585
  		int id = UBI_VOL_NUM_AUTO;
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
586
587
588
  
  		/* Use maximum available size */
  		size = 0;
386f20cad   Quentin Schulz   ubi: provide a wa...
589
590
591
592
593
  		/* E.g., create volume with "skipcheck" bit set */
  		if (argc == 7) {
  			skipcheck = strncmp(argv[6], "--skipcheck", 11) == 0;
  			argc--;
  		}
006124223   Ladislav Michl   cmd: ubi: add opt...
594
595
596
597
598
  		/* E.g., create volume size type vol_id */
  		if (argc == 6) {
  			id = simple_strtoull(argv[5], NULL, 16);
  			argc--;
  		}
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
599
600
601
602
603
604
605
606
607
608
609
610
611
  		/* 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) {
f59f07ece   Ladislav Michl   cmd: ubi: allow '...
612
613
  			if (argv[3][0] != '-')
  				size = simple_strtoull(argv[3], NULL, 16);
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
614
615
616
  			argc--;
  		}
  		/* Use maximum available size */
7f5d8a4d8   Stefan Roese   UBI: Fix error co...
617
  		if (!size) {
dd7185f17   Paul Burton   cmd_ubi: use int6...
618
619
620
  			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...
621
  		}
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
622
  		/* E.g., create volume */
386f20cad   Quentin Schulz   ubi: provide a wa...
623
624
625
626
  		if (argc == 3) {
  			return ubi_create_vol(argv[2], size, dynamic, id,
  					      skipcheck);
  		}
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
627
628
629
630
631
632
633
  	}
  
  	if (strncmp(argv[1], "remove", 6) == 0) {
  		/* E.g., remove volume */
  		if (argc == 3)
  			return ubi_remove_vol(argv[2]);
  	}
83f7078b6   Philippe Reynes   cmd: ubi: add a c...
634
635
  	if (IS_ENABLED(CONFIG_CMD_UBI_RENAME) && !strncmp(argv[1], "rename", 6))
  		return ubi_rename_vol(argv[2], argv[3]);
e6661cf76   Stefan Roese   ubi: Add "skipche...
636
637
638
639
640
641
642
  	if (strncmp(argv[1], "skipcheck", 9) == 0) {
  		/* E.g., change skip_check flag */
  		if (argc == 4) {
  			skipcheck = strncmp(argv[3], "on", 2) == 0;
  			return ubi_set_skip_check(argv[2], skipcheck);
  		}
  	}
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
643
  	if (strncmp(argv[1], "write", 5) == 0) {
718290675   Joe Hershberger   ubi: Expose a few...
644
  		int ret;
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
645
646
647
648
649
  		if (argc < 5) {
  			printf("Please see usage
  ");
  			return 1;
  		}
7e5f460ec   Simon Glass   global: Convert s...
650
651
  		addr = hextoul(argv[2], NULL);
  		size = hextoul(argv[4], NULL);
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
652

cc734f5ab   Paul Burton   cmd_ubi: add writ...
653
654
655
656
657
658
659
  		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;
7e5f460ec   Simon Glass   global: Convert s...
660
  				full_size = hextoul(argv[5], NULL);
cc734f5ab   Paul Burton   cmd_ubi: add writ...
661
662
663
664
665
666
  				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...
667
  		if (!ret) {
dd7185f17   Paul Burton   cmd_ubi: use int6...
668
669
  			printf("%lld bytes written to volume %s
  ", size,
718290675   Joe Hershberger   ubi: Expose a few...
670
671
672
673
  			       argv[3]);
  		}
  
  		return ret;
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
674
675
676
677
678
679
680
  	}
  
  	if (strncmp(argv[1], "read", 4) == 0) {
  		size = 0;
  
  		/* E.g., read volume size */
  		if (argc == 5) {
7e5f460ec   Simon Glass   global: Convert s...
681
  			size = hextoul(argv[4], NULL);
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
682
683
684
685
686
  			argc--;
  		}
  
  		/* E.g., read volume */
  		if (argc == 4) {
7e5f460ec   Simon Glass   global: Convert s...
687
  			addr = hextoul(argv[2], NULL);
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
688
689
  			argc--;
  		}
718290675   Joe Hershberger   ubi: Expose a few...
690
  		if (argc == 3) {
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
691
  			return ubi_volume_read(argv[3], (char *)addr, size);
718290675   Joe Hershberger   ubi: Expose a few...
692
  		}
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
693
694
695
696
  	}
  
  	printf("Please see usage
  ");
7f5d8a4d8   Stefan Roese   UBI: Fix error co...
697
  	return 1;
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
698
  }
388a29d02   Frans Meulenbroeks   various cmd_* fil...
699
  U_BOOT_CMD(
386f20cad   Quentin Schulz   ubi: provide a wa...
700
  	ubi, 7, 1, do_ubi,
2fb2604d5   Peter Tyser   Command usage cle...
701
  	"ubi commands",
cddfc97d1   Heiko Schocher   ubi: add new ubi ...
702
703
704
705
706
  	"detach"
  		" - detach ubi from a mtd partition
  "
  	"ubi part [part] [offset]
  "
25c8f4005   Simon Kagstrom   Handle VID header...
707
708
709
  		" - Show or set current partition (with optional VID"
  		" header offset)
  "
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
710
711
712
  	"ubi info [l[ayout]]"
  		" - Display volume and ubi layout information
  "
f9f4d809a   Heiko Schocher   common, ubi: add ...
713
714
715
  	"ubi check volumename"
  		" - check if volumename exists
  "
386f20cad   Quentin Schulz   ubi: provide a wa...
716
717
  	"ubi create[vol] volume [size] [type] [id] [--skipcheck]
  "
f59f07ece   Ladislav Michl   cmd: ubi: allow '...
718
719
720
  		" - create volume name with size ('-' for maximum"
  		" available size)
  "
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
721
722
723
  	"ubi write[vol] address volume size"
  		" - Write volume from address with size
  "
cc734f5ab   Paul Burton   cmd_ubi: add writ...
724
725
726
727
  	"ubi write.part address volume size [fullsize]
  "
  		" - Write part of a volume from address
  "
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
728
729
730
731
732
733
  	"ubi read[vol] address volume [size]"
  		" - Read volume to address with size
  "
  	"ubi remove[vol] volume"
  		" - Remove volume
  "
83f7078b6   Philippe Reynes   cmd: ubi: add a c...
734
735
736
737
  #if IS_ENABLED(CONFIG_CMD_UBI_RENAME)
  	"ubi rename oldname newname
  "
  #endif
e6661cf76   Stefan Roese   ubi: Add "skipche...
738
739
  	"ubi skipcheck volume on/off - Set or clear skip_check flag in volume header
  "
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
740
741
  	"[Legends]
  "
f6ca3b709   Andrzej Wolski   ubi: help message...
742
743
744
745
  	" volume: character name
  "
  	" size: specified in bytes
  "
a89c33db9   Wolfgang Denk   General help mess...
746
  	" type: s[tatic] or d[ynamic] (default=dynamic)"
694a0b3f1   Kyungmin Park   UBI: Add UBI comm...
747
  );