mtdparts.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106
  1. /*
  2. * (C) Copyright 2002
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * (C) Copyright 2002
  6. * Robert Schwebel, Pengutronix, <r.schwebel@pengutronix.de>
  7. *
  8. * (C) Copyright 2003
  9. * Kai-Uwe Bloem, Auerswald GmbH & Co KG, <linux-development@auerswald.de>
  10. *
  11. * (C) Copyright 2005
  12. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  13. *
  14. * Added support for reading flash partition table from environment.
  15. * Parsing routines are based on driver/mtd/cmdline.c from the linux 2.4
  16. * kernel tree.
  17. *
  18. * (C) Copyright 2008
  19. * Harald Welte, OpenMoko, Inc., Harald Welte <laforge@openmoko.org>
  20. *
  21. * $Id: cmdlinepart.c,v 1.17 2004/11/26 11:18:47 lavinen Exp $
  22. * Copyright 2002 SYSGO Real-Time Solutions GmbH
  23. *
  24. * SPDX-License-Identifier: GPL-2.0+
  25. */
  26. /*
  27. * Three environment variables are used by the parsing routines:
  28. *
  29. * 'partition' - keeps current partition identifier
  30. *
  31. * partition := <part-id>
  32. * <part-id> := <dev-id>,part_num
  33. *
  34. *
  35. * 'mtdids' - linux kernel mtd device id <-> u-boot device id mapping
  36. *
  37. * mtdids=<idmap>[,<idmap>,...]
  38. *
  39. * <idmap> := <dev-id>=<mtd-id>
  40. * <dev-id> := 'nand'|'nor'|'onenand'<dev-num>
  41. * <dev-num> := mtd device number, 0...
  42. * <mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)
  43. *
  44. *
  45. * 'mtdparts' - partition list
  46. *
  47. * mtdparts=mtdparts=<mtd-def>[;<mtd-def>...]
  48. *
  49. * <mtd-def> := <mtd-id>:<part-def>[,<part-def>...]
  50. * <mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)
  51. * <part-def> := <size>[@<offset>][<name>][<ro-flag>]
  52. * <size> := standard linux memsize OR '-' to denote all remaining space
  53. * <offset> := partition start offset within the device
  54. * <name> := '(' NAME ')'
  55. * <ro-flag> := when set to 'ro' makes partition read-only (not used, passed to kernel)
  56. *
  57. * Notes:
  58. * - each <mtd-id> used in mtdparts must albo exist in 'mtddis' mapping
  59. * - if the above variables are not set defaults for a given target are used
  60. *
  61. * Examples:
  62. *
  63. * 1 NOR Flash, with 1 single writable partition:
  64. * mtdids=nor0=edb7312-nor
  65. * mtdparts=mtdparts=edb7312-nor:-
  66. *
  67. * 1 NOR Flash with 2 partitions, 1 NAND with one
  68. * mtdids=nor0=edb7312-nor,nand0=edb7312-nand
  69. * mtdparts=mtdparts=edb7312-nor:256k(ARMboot)ro,-(root);edb7312-nand:-(home)
  70. *
  71. */
  72. #include <common.h>
  73. #include <command.h>
  74. #include <malloc.h>
  75. #include <jffs2/load_kernel.h>
  76. #include <linux/list.h>
  77. #include <linux/ctype.h>
  78. #include <linux/err.h>
  79. #include <linux/mtd/mtd.h>
  80. #if defined(CONFIG_CMD_NAND)
  81. #include <linux/mtd/nand.h>
  82. #include <nand.h>
  83. #endif
  84. #if defined(CONFIG_CMD_ONENAND)
  85. #include <linux/mtd/onenand.h>
  86. #include <onenand_uboot.h>
  87. #endif
  88. DECLARE_GLOBAL_DATA_PTR;
  89. /* special size referring to all the remaining space in a partition */
  90. #define SIZE_REMAINING (~0llu)
  91. /* special offset value, it is used when not provided by user
  92. *
  93. * this value is used temporarily during parsing, later such offests
  94. * are recalculated */
  95. #define OFFSET_NOT_SPECIFIED (~0llu)
  96. /* minimum partition size */
  97. #define MIN_PART_SIZE 4096
  98. /* this flag needs to be set in part_info struct mask_flags
  99. * field for read-only partitions */
  100. #define MTD_WRITEABLE_CMD 1
  101. /* default values for mtdids and mtdparts variables */
  102. #if defined(MTDIDS_DEFAULT)
  103. static const char *const mtdids_default = MTDIDS_DEFAULT;
  104. #else
  105. static const char *const mtdids_default = NULL;
  106. #endif
  107. #if defined(MTDPARTS_DEFAULT)
  108. static const char *const mtdparts_default = MTDPARTS_DEFAULT;
  109. #else
  110. static const char *const mtdparts_default = NULL;
  111. #endif
  112. /* copies of last seen 'mtdids', 'mtdparts' and 'partition' env variables */
  113. #define MTDIDS_MAXLEN 128
  114. #define MTDPARTS_MAXLEN 512
  115. #define PARTITION_MAXLEN 16
  116. static char last_ids[MTDIDS_MAXLEN];
  117. static char last_parts[MTDPARTS_MAXLEN];
  118. static char last_partition[PARTITION_MAXLEN];
  119. /* low level jffs2 cache cleaning routine */
  120. extern void jffs2_free_cache(struct part_info *part);
  121. /* mtdids mapping list, filled by parse_ids() */
  122. static struct list_head mtdids;
  123. /* device/partition list, parse_cmdline() parses into here */
  124. static struct list_head devices;
  125. /* current active device and partition number */
  126. struct mtd_device *current_mtd_dev = NULL;
  127. u8 current_mtd_partnum = 0;
  128. static struct part_info* mtd_part_info(struct mtd_device *dev, unsigned int part_num);
  129. /* command line only routines */
  130. static struct mtdids* id_find_by_mtd_id(const char *mtd_id, unsigned int mtd_id_len);
  131. static int device_del(struct mtd_device *dev);
  132. /**
  133. * Parses a string into a number. The number stored at ptr is
  134. * potentially suffixed with K (for kilobytes, or 1024 bytes),
  135. * M (for megabytes, or 1048576 bytes), or G (for gigabytes, or
  136. * 1073741824). If the number is suffixed with K, M, or G, then
  137. * the return value is the number multiplied by one kilobyte, one
  138. * megabyte, or one gigabyte, respectively.
  139. *
  140. * @param ptr where parse begins
  141. * @param retptr output pointer to next char after parse completes (output)
  142. * @return resulting unsigned int
  143. */
  144. static u64 memsize_parse (const char *const ptr, const char **retptr)
  145. {
  146. u64 ret = simple_strtoull(ptr, (char **)retptr, 0);
  147. switch (**retptr) {
  148. case 'G':
  149. case 'g':
  150. ret <<= 10;
  151. case 'M':
  152. case 'm':
  153. ret <<= 10;
  154. case 'K':
  155. case 'k':
  156. ret <<= 10;
  157. (*retptr)++;
  158. default:
  159. break;
  160. }
  161. return ret;
  162. }
  163. /**
  164. * Format string describing supplied size. This routine does the opposite job
  165. * to memsize_parse(). Size in bytes is converted to string and if possible
  166. * shortened by using k (kilobytes), m (megabytes) or g (gigabytes) suffix.
  167. *
  168. * Note, that this routine does not check for buffer overflow, it's the caller
  169. * who must assure enough space.
  170. *
  171. * @param buf output buffer
  172. * @param size size to be converted to string
  173. */
  174. static void memsize_format(char *buf, u64 size)
  175. {
  176. #define SIZE_GB ((u32)1024*1024*1024)
  177. #define SIZE_MB ((u32)1024*1024)
  178. #define SIZE_KB ((u32)1024)
  179. if ((size % SIZE_GB) == 0)
  180. sprintf(buf, "%llug", size/SIZE_GB);
  181. else if ((size % SIZE_MB) == 0)
  182. sprintf(buf, "%llum", size/SIZE_MB);
  183. else if (size % SIZE_KB == 0)
  184. sprintf(buf, "%lluk", size/SIZE_KB);
  185. else
  186. sprintf(buf, "%llu", size);
  187. }
  188. /**
  189. * This routine does global indexing of all partitions. Resulting index for
  190. * current partition is saved in 'mtddevnum'. Current partition name in
  191. * 'mtddevname'.
  192. */
  193. static void index_partitions(void)
  194. {
  195. u16 mtddevnum;
  196. struct part_info *part;
  197. struct list_head *dentry;
  198. struct mtd_device *dev;
  199. debug("--- index partitions ---\n");
  200. if (current_mtd_dev) {
  201. mtddevnum = 0;
  202. list_for_each(dentry, &devices) {
  203. dev = list_entry(dentry, struct mtd_device, link);
  204. if (dev == current_mtd_dev) {
  205. mtddevnum += current_mtd_partnum;
  206. setenv_ulong("mtddevnum", mtddevnum);
  207. break;
  208. }
  209. mtddevnum += dev->num_parts;
  210. }
  211. part = mtd_part_info(current_mtd_dev, current_mtd_partnum);
  212. setenv("mtddevname", part->name);
  213. debug("=> mtddevnum %d,\n=> mtddevname %s\n", mtddevnum, part->name);
  214. } else {
  215. setenv("mtddevnum", NULL);
  216. setenv("mtddevname", NULL);
  217. debug("=> mtddevnum NULL\n=> mtddevname NULL\n");
  218. }
  219. }
  220. /**
  221. * Save current device and partition in environment variable 'partition'.
  222. */
  223. static void current_save(void)
  224. {
  225. char buf[16];
  226. debug("--- current_save ---\n");
  227. if (current_mtd_dev) {
  228. sprintf(buf, "%s%d,%d", MTD_DEV_TYPE(current_mtd_dev->id->type),
  229. current_mtd_dev->id->num, current_mtd_partnum);
  230. setenv("partition", buf);
  231. strncpy(last_partition, buf, 16);
  232. debug("=> partition %s\n", buf);
  233. } else {
  234. setenv("partition", NULL);
  235. last_partition[0] = '\0';
  236. debug("=> partition NULL\n");
  237. }
  238. index_partitions();
  239. }
  240. /**
  241. * Produce a mtd_info given a type and num.
  242. *
  243. * @param type mtd type
  244. * @param num mtd number
  245. * @param mtd a pointer to an mtd_info instance (output)
  246. * @return 0 if device is valid, 1 otherwise
  247. */
  248. static int get_mtd_info(u8 type, u8 num, struct mtd_info **mtd)
  249. {
  250. char mtd_dev[16];
  251. sprintf(mtd_dev, "%s%d", MTD_DEV_TYPE(type), num);
  252. *mtd = get_mtd_device_nm(mtd_dev);
  253. if (IS_ERR(*mtd)) {
  254. printf("Device %s not found!\n", mtd_dev);
  255. return 1;
  256. }
  257. put_mtd_device(*mtd);
  258. return 0;
  259. }
  260. /**
  261. * Performs sanity check for supplied flash partition.
  262. * Table of existing MTD flash devices is searched and partition device
  263. * is located. Alignment with the granularity of nand erasesize is verified.
  264. *
  265. * @param id of the parent device
  266. * @param part partition to validate
  267. * @return 0 if partition is valid, 1 otherwise
  268. */
  269. static int part_validate_eraseblock(struct mtdids *id, struct part_info *part)
  270. {
  271. struct mtd_info *mtd = NULL;
  272. int i, j;
  273. ulong start;
  274. u64 offset, size;
  275. if (get_mtd_info(id->type, id->num, &mtd))
  276. return 1;
  277. part->sector_size = mtd->erasesize;
  278. if (!mtd->numeraseregions) {
  279. /*
  280. * Only one eraseregion (NAND, OneNAND or uniform NOR),
  281. * checking for alignment is easy here
  282. */
  283. offset = part->offset;
  284. if (do_div(offset, mtd->erasesize)) {
  285. printf("%s%d: partition (%s) start offset"
  286. "alignment incorrect\n",
  287. MTD_DEV_TYPE(id->type), id->num, part->name);
  288. return 1;
  289. }
  290. size = part->size;
  291. if (do_div(size, mtd->erasesize)) {
  292. printf("%s%d: partition (%s) size alignment incorrect\n",
  293. MTD_DEV_TYPE(id->type), id->num, part->name);
  294. return 1;
  295. }
  296. } else {
  297. /*
  298. * Multiple eraseregions (non-uniform NOR),
  299. * checking for alignment is more complex here
  300. */
  301. /* Check start alignment */
  302. for (i = 0; i < mtd->numeraseregions; i++) {
  303. start = mtd->eraseregions[i].offset;
  304. for (j = 0; j < mtd->eraseregions[i].numblocks; j++) {
  305. if (part->offset == start)
  306. goto start_ok;
  307. start += mtd->eraseregions[i].erasesize;
  308. }
  309. }
  310. printf("%s%d: partition (%s) start offset alignment incorrect\n",
  311. MTD_DEV_TYPE(id->type), id->num, part->name);
  312. return 1;
  313. start_ok:
  314. /* Check end/size alignment */
  315. for (i = 0; i < mtd->numeraseregions; i++) {
  316. start = mtd->eraseregions[i].offset;
  317. for (j = 0; j < mtd->eraseregions[i].numblocks; j++) {
  318. if ((part->offset + part->size) == start)
  319. goto end_ok;
  320. start += mtd->eraseregions[i].erasesize;
  321. }
  322. }
  323. /* Check last sector alignment */
  324. if ((part->offset + part->size) == start)
  325. goto end_ok;
  326. printf("%s%d: partition (%s) size alignment incorrect\n",
  327. MTD_DEV_TYPE(id->type), id->num, part->name);
  328. return 1;
  329. end_ok:
  330. return 0;
  331. }
  332. return 0;
  333. }
  334. /**
  335. * Performs sanity check for supplied partition. Offset and size are
  336. * verified to be within valid range. Partition type is checked and
  337. * part_validate_eraseblock() is called with the argument of part.
  338. *
  339. * @param id of the parent device
  340. * @param part partition to validate
  341. * @return 0 if partition is valid, 1 otherwise
  342. */
  343. static int part_validate(struct mtdids *id, struct part_info *part)
  344. {
  345. if (part->size == SIZE_REMAINING)
  346. part->size = id->size - part->offset;
  347. if (part->offset > id->size) {
  348. printf("%s: offset %08llx beyond flash size %08llx\n",
  349. id->mtd_id, part->offset, id->size);
  350. return 1;
  351. }
  352. if ((part->offset + part->size) <= part->offset) {
  353. printf("%s%d: partition (%s) size too big\n",
  354. MTD_DEV_TYPE(id->type), id->num, part->name);
  355. return 1;
  356. }
  357. if (part->offset + part->size > id->size) {
  358. printf("%s: partitioning exceeds flash size\n", id->mtd_id);
  359. return 1;
  360. }
  361. /*
  362. * Now we need to check if the partition starts and ends on
  363. * sector (eraseblock) regions
  364. */
  365. return part_validate_eraseblock(id, part);
  366. }
  367. /**
  368. * Delete selected partition from the partition list of the specified device.
  369. *
  370. * @param dev device to delete partition from
  371. * @param part partition to delete
  372. * @return 0 on success, 1 otherwise
  373. */
  374. static int part_del(struct mtd_device *dev, struct part_info *part)
  375. {
  376. u8 current_save_needed = 0;
  377. /* if there is only one partition, remove whole device */
  378. if (dev->num_parts == 1)
  379. return device_del(dev);
  380. /* otherwise just delete this partition */
  381. if (dev == current_mtd_dev) {
  382. /* we are modyfing partitions for the current device,
  383. * update current */
  384. struct part_info *curr_pi;
  385. curr_pi = mtd_part_info(current_mtd_dev, current_mtd_partnum);
  386. if (curr_pi) {
  387. if (curr_pi == part) {
  388. printf("current partition deleted, resetting current to 0\n");
  389. current_mtd_partnum = 0;
  390. } else if (part->offset <= curr_pi->offset) {
  391. current_mtd_partnum--;
  392. }
  393. current_save_needed = 1;
  394. }
  395. }
  396. list_del(&part->link);
  397. free(part);
  398. dev->num_parts--;
  399. if (current_save_needed > 0)
  400. current_save();
  401. else
  402. index_partitions();
  403. return 0;
  404. }
  405. /**
  406. * Delete all partitions from parts head list, free memory.
  407. *
  408. * @param head list of partitions to delete
  409. */
  410. static void part_delall(struct list_head *head)
  411. {
  412. struct list_head *entry, *n;
  413. struct part_info *part_tmp;
  414. /* clean tmp_list and free allocated memory */
  415. list_for_each_safe(entry, n, head) {
  416. part_tmp = list_entry(entry, struct part_info, link);
  417. list_del(entry);
  418. free(part_tmp);
  419. }
  420. }
  421. /**
  422. * Add new partition to the supplied partition list. Make sure partitions are
  423. * sorted by offset in ascending order.
  424. *
  425. * @param head list this partition is to be added to
  426. * @param new partition to be added
  427. */
  428. static int part_sort_add(struct mtd_device *dev, struct part_info *part)
  429. {
  430. struct list_head *entry;
  431. struct part_info *new_pi, *curr_pi;
  432. /* link partition to parrent dev */
  433. part->dev = dev;
  434. if (list_empty(&dev->parts)) {
  435. debug("part_sort_add: list empty\n");
  436. list_add(&part->link, &dev->parts);
  437. dev->num_parts++;
  438. index_partitions();
  439. return 0;
  440. }
  441. new_pi = list_entry(&part->link, struct part_info, link);
  442. /* get current partition info if we are updating current device */
  443. curr_pi = NULL;
  444. if (dev == current_mtd_dev)
  445. curr_pi = mtd_part_info(current_mtd_dev, current_mtd_partnum);
  446. list_for_each(entry, &dev->parts) {
  447. struct part_info *pi;
  448. pi = list_entry(entry, struct part_info, link);
  449. /* be compliant with kernel cmdline, allow only one partition at offset zero */
  450. if ((new_pi->offset == pi->offset) && (pi->offset == 0)) {
  451. printf("cannot add second partition at offset 0\n");
  452. return 1;
  453. }
  454. if (new_pi->offset <= pi->offset) {
  455. list_add_tail(&part->link, entry);
  456. dev->num_parts++;
  457. if (curr_pi && (pi->offset <= curr_pi->offset)) {
  458. /* we are modyfing partitions for the current
  459. * device, update current */
  460. current_mtd_partnum++;
  461. current_save();
  462. } else {
  463. index_partitions();
  464. }
  465. return 0;
  466. }
  467. }
  468. list_add_tail(&part->link, &dev->parts);
  469. dev->num_parts++;
  470. index_partitions();
  471. return 0;
  472. }
  473. /**
  474. * Add provided partition to the partition list of a given device.
  475. *
  476. * @param dev device to which partition is added
  477. * @param part partition to be added
  478. * @return 0 on success, 1 otherwise
  479. */
  480. static int part_add(struct mtd_device *dev, struct part_info *part)
  481. {
  482. /* verify alignment and size */
  483. if (part_validate(dev->id, part) != 0)
  484. return 1;
  485. /* partition is ok, add it to the list */
  486. if (part_sort_add(dev, part) != 0)
  487. return 1;
  488. return 0;
  489. }
  490. /**
  491. * Parse one partition definition, allocate memory and return pointer to this
  492. * location in retpart.
  493. *
  494. * @param partdef pointer to the partition definition string i.e. <part-def>
  495. * @param ret output pointer to next char after parse completes (output)
  496. * @param retpart pointer to the allocated partition (output)
  497. * @return 0 on success, 1 otherwise
  498. */
  499. static int part_parse(const char *const partdef, const char **ret, struct part_info **retpart)
  500. {
  501. struct part_info *part;
  502. u64 size;
  503. u64 offset;
  504. const char *name;
  505. int name_len;
  506. unsigned int mask_flags;
  507. const char *p;
  508. p = partdef;
  509. *retpart = NULL;
  510. *ret = NULL;
  511. /* fetch the partition size */
  512. if (*p == '-') {
  513. /* assign all remaining space to this partition */
  514. debug("'-': remaining size assigned\n");
  515. size = SIZE_REMAINING;
  516. p++;
  517. } else {
  518. size = memsize_parse(p, &p);
  519. if (size < MIN_PART_SIZE) {
  520. printf("partition size too small (%llx)\n", size);
  521. return 1;
  522. }
  523. }
  524. /* check for offset */
  525. offset = OFFSET_NOT_SPECIFIED;
  526. if (*p == '@') {
  527. p++;
  528. offset = memsize_parse(p, &p);
  529. }
  530. /* now look for the name */
  531. if (*p == '(') {
  532. name = ++p;
  533. if ((p = strchr(name, ')')) == NULL) {
  534. printf("no closing ) found in partition name\n");
  535. return 1;
  536. }
  537. name_len = p - name + 1;
  538. if ((name_len - 1) == 0) {
  539. printf("empty partition name\n");
  540. return 1;
  541. }
  542. p++;
  543. } else {
  544. /* 0x00000000@0x00000000 */
  545. name_len = 22;
  546. name = NULL;
  547. }
  548. /* test for options */
  549. mask_flags = 0;
  550. if (strncmp(p, "ro", 2) == 0) {
  551. mask_flags |= MTD_WRITEABLE_CMD;
  552. p += 2;
  553. }
  554. /* check for next partition definition */
  555. if (*p == ',') {
  556. if (size == SIZE_REMAINING) {
  557. *ret = NULL;
  558. printf("no partitions allowed after a fill-up partition\n");
  559. return 1;
  560. }
  561. *ret = ++p;
  562. } else if ((*p == ';') || (*p == '\0')) {
  563. *ret = p;
  564. } else {
  565. printf("unexpected character '%c' at the end of partition\n", *p);
  566. *ret = NULL;
  567. return 1;
  568. }
  569. /* allocate memory */
  570. part = (struct part_info *)malloc(sizeof(struct part_info) + name_len);
  571. if (!part) {
  572. printf("out of memory\n");
  573. return 1;
  574. }
  575. memset(part, 0, sizeof(struct part_info) + name_len);
  576. part->size = size;
  577. part->offset = offset;
  578. part->mask_flags = mask_flags;
  579. part->name = (char *)(part + 1);
  580. if (name) {
  581. /* copy user provided name */
  582. strncpy(part->name, name, name_len - 1);
  583. part->auto_name = 0;
  584. } else {
  585. /* auto generated name in form of size@offset */
  586. sprintf(part->name, "0x%08llx@0x%08llx", size, offset);
  587. part->auto_name = 1;
  588. }
  589. part->name[name_len - 1] = '\0';
  590. INIT_LIST_HEAD(&part->link);
  591. debug("+ partition: name %-22s size 0x%08llx offset 0x%08llx mask flags %d\n",
  592. part->name, part->size,
  593. part->offset, part->mask_flags);
  594. *retpart = part;
  595. return 0;
  596. }
  597. /**
  598. * Check device number to be within valid range for given device type.
  599. *
  600. * @param type mtd type
  601. * @param num mtd number
  602. * @param size a pointer to the size of the mtd device (output)
  603. * @return 0 if device is valid, 1 otherwise
  604. */
  605. static int mtd_device_validate(u8 type, u8 num, u64 *size)
  606. {
  607. struct mtd_info *mtd = NULL;
  608. if (get_mtd_info(type, num, &mtd))
  609. return 1;
  610. *size = mtd->size;
  611. return 0;
  612. }
  613. /**
  614. * Delete all mtd devices from a supplied devices list, free memory allocated for
  615. * each device and delete all device partitions.
  616. *
  617. * @return 0 on success, 1 otherwise
  618. */
  619. static int device_delall(struct list_head *head)
  620. {
  621. struct list_head *entry, *n;
  622. struct mtd_device *dev_tmp;
  623. /* clean devices list */
  624. list_for_each_safe(entry, n, head) {
  625. dev_tmp = list_entry(entry, struct mtd_device, link);
  626. list_del(entry);
  627. part_delall(&dev_tmp->parts);
  628. free(dev_tmp);
  629. }
  630. INIT_LIST_HEAD(&devices);
  631. return 0;
  632. }
  633. /**
  634. * If provided device exists it's partitions are deleted, device is removed
  635. * from device list and device memory is freed.
  636. *
  637. * @param dev device to be deleted
  638. * @return 0 on success, 1 otherwise
  639. */
  640. static int device_del(struct mtd_device *dev)
  641. {
  642. part_delall(&dev->parts);
  643. list_del(&dev->link);
  644. free(dev);
  645. if (dev == current_mtd_dev) {
  646. /* we just deleted current device */
  647. if (list_empty(&devices)) {
  648. current_mtd_dev = NULL;
  649. } else {
  650. /* reset first partition from first dev from the
  651. * devices list as current */
  652. current_mtd_dev = list_entry(devices.next, struct mtd_device, link);
  653. current_mtd_partnum = 0;
  654. }
  655. current_save();
  656. return 0;
  657. }
  658. index_partitions();
  659. return 0;
  660. }
  661. /**
  662. * Search global device list and return pointer to the device of type and num
  663. * specified.
  664. *
  665. * @param type device type
  666. * @param num device number
  667. * @return NULL if requested device does not exist
  668. */
  669. struct mtd_device *device_find(u8 type, u8 num)
  670. {
  671. struct list_head *entry;
  672. struct mtd_device *dev_tmp;
  673. list_for_each(entry, &devices) {
  674. dev_tmp = list_entry(entry, struct mtd_device, link);
  675. if ((dev_tmp->id->type == type) && (dev_tmp->id->num == num))
  676. return dev_tmp;
  677. }
  678. return NULL;
  679. }
  680. /**
  681. * Add specified device to the global device list.
  682. *
  683. * @param dev device to be added
  684. */
  685. static void device_add(struct mtd_device *dev)
  686. {
  687. u8 current_save_needed = 0;
  688. if (list_empty(&devices)) {
  689. current_mtd_dev = dev;
  690. current_mtd_partnum = 0;
  691. current_save_needed = 1;
  692. }
  693. list_add_tail(&dev->link, &devices);
  694. if (current_save_needed > 0)
  695. current_save();
  696. else
  697. index_partitions();
  698. }
  699. /**
  700. * Parse device type, name and mtd-id. If syntax is ok allocate memory and
  701. * return pointer to the device structure.
  702. *
  703. * @param mtd_dev pointer to the device definition string i.e. <mtd-dev>
  704. * @param ret output pointer to next char after parse completes (output)
  705. * @param retdev pointer to the allocated device (output)
  706. * @return 0 on success, 1 otherwise
  707. */
  708. static int device_parse(const char *const mtd_dev, const char **ret, struct mtd_device **retdev)
  709. {
  710. struct mtd_device *dev;
  711. struct part_info *part;
  712. struct mtdids *id;
  713. const char *mtd_id;
  714. unsigned int mtd_id_len;
  715. const char *p;
  716. const char *pend;
  717. LIST_HEAD(tmp_list);
  718. struct list_head *entry, *n;
  719. u16 num_parts;
  720. u64 offset;
  721. int err = 1;
  722. debug("===device_parse===\n");
  723. assert(retdev);
  724. *retdev = NULL;
  725. if (ret)
  726. *ret = NULL;
  727. /* fetch <mtd-id> */
  728. mtd_id = p = mtd_dev;
  729. if (!(p = strchr(mtd_id, ':'))) {
  730. printf("no <mtd-id> identifier\n");
  731. return 1;
  732. }
  733. mtd_id_len = p - mtd_id + 1;
  734. p++;
  735. /* verify if we have a valid device specified */
  736. if ((id = id_find_by_mtd_id(mtd_id, mtd_id_len - 1)) == NULL) {
  737. printf("invalid mtd device '%.*s'\n", mtd_id_len - 1, mtd_id);
  738. return 1;
  739. }
  740. #ifdef DEBUG
  741. pend = strchr(p, ';');
  742. #endif
  743. debug("dev type = %d (%s), dev num = %d, mtd-id = %s\n",
  744. id->type, MTD_DEV_TYPE(id->type),
  745. id->num, id->mtd_id);
  746. debug("parsing partitions %.*s\n", (int)(pend ? pend - p : strlen(p)), p);
  747. /* parse partitions */
  748. num_parts = 0;
  749. offset = 0;
  750. if ((dev = device_find(id->type, id->num)) != NULL) {
  751. /* if device already exists start at the end of the last partition */
  752. part = list_entry(dev->parts.prev, struct part_info, link);
  753. offset = part->offset + part->size;
  754. }
  755. while (p && (*p != '\0') && (*p != ';')) {
  756. err = 1;
  757. if ((part_parse(p, &p, &part) != 0) || (!part))
  758. break;
  759. /* calculate offset when not specified */
  760. if (part->offset == OFFSET_NOT_SPECIFIED)
  761. part->offset = offset;
  762. else
  763. offset = part->offset;
  764. /* verify alignment and size */
  765. if (part_validate(id, part) != 0)
  766. break;
  767. offset += part->size;
  768. /* partition is ok, add it to the list */
  769. list_add_tail(&part->link, &tmp_list);
  770. num_parts++;
  771. err = 0;
  772. }
  773. if (err == 1) {
  774. part_delall(&tmp_list);
  775. return 1;
  776. }
  777. if (num_parts == 0) {
  778. printf("no partitions for device %s%d (%s)\n",
  779. MTD_DEV_TYPE(id->type), id->num, id->mtd_id);
  780. return 1;
  781. }
  782. debug("\ntotal partitions: %d\n", num_parts);
  783. /* check for next device presence */
  784. if (p) {
  785. if (*p == ';') {
  786. if (ret)
  787. *ret = ++p;
  788. } else if (*p == '\0') {
  789. if (ret)
  790. *ret = p;
  791. } else {
  792. printf("unexpected character '%c' at the end of device\n", *p);
  793. if (ret)
  794. *ret = NULL;
  795. return 1;
  796. }
  797. }
  798. /* allocate memory for mtd_device structure */
  799. if ((dev = (struct mtd_device *)malloc(sizeof(struct mtd_device))) == NULL) {
  800. printf("out of memory\n");
  801. return 1;
  802. }
  803. memset(dev, 0, sizeof(struct mtd_device));
  804. dev->id = id;
  805. dev->num_parts = 0; /* part_sort_add increments num_parts */
  806. INIT_LIST_HEAD(&dev->parts);
  807. INIT_LIST_HEAD(&dev->link);
  808. /* move partitions from tmp_list to dev->parts */
  809. list_for_each_safe(entry, n, &tmp_list) {
  810. part = list_entry(entry, struct part_info, link);
  811. list_del(entry);
  812. if (part_sort_add(dev, part) != 0) {
  813. device_del(dev);
  814. return 1;
  815. }
  816. }
  817. *retdev = dev;
  818. debug("===\n\n");
  819. return 0;
  820. }
  821. /**
  822. * Initialize global device list.
  823. *
  824. * @return 0 on success, 1 otherwise
  825. */
  826. static int mtd_devices_init(void)
  827. {
  828. last_parts[0] = '\0';
  829. current_mtd_dev = NULL;
  830. current_save();
  831. return device_delall(&devices);
  832. }
  833. /*
  834. * Search global mtdids list and find id of requested type and number.
  835. *
  836. * @return pointer to the id if it exists, NULL otherwise
  837. */
  838. static struct mtdids* id_find(u8 type, u8 num)
  839. {
  840. struct list_head *entry;
  841. struct mtdids *id;
  842. list_for_each(entry, &mtdids) {
  843. id = list_entry(entry, struct mtdids, link);
  844. if ((id->type == type) && (id->num == num))
  845. return id;
  846. }
  847. return NULL;
  848. }
  849. /**
  850. * Search global mtdids list and find id of a requested mtd_id.
  851. *
  852. * Note: first argument is not null terminated.
  853. *
  854. * @param mtd_id string containing requested mtd_id
  855. * @param mtd_id_len length of supplied mtd_id
  856. * @return pointer to the id if it exists, NULL otherwise
  857. */
  858. static struct mtdids* id_find_by_mtd_id(const char *mtd_id, unsigned int mtd_id_len)
  859. {
  860. struct list_head *entry;
  861. struct mtdids *id;
  862. debug("--- id_find_by_mtd_id: '%.*s' (len = %d)\n",
  863. mtd_id_len, mtd_id, mtd_id_len);
  864. list_for_each(entry, &mtdids) {
  865. id = list_entry(entry, struct mtdids, link);
  866. debug("entry: '%s' (len = %zu)\n",
  867. id->mtd_id, strlen(id->mtd_id));
  868. if (mtd_id_len != strlen(id->mtd_id))
  869. continue;
  870. if (strncmp(id->mtd_id, mtd_id, mtd_id_len) == 0)
  871. return id;
  872. }
  873. return NULL;
  874. }
  875. /**
  876. * Parse device id string <dev-id> := 'nand'|'nor'|'onenand'<dev-num>,
  877. * return device type and number.
  878. *
  879. * @param id string describing device id
  880. * @param ret_id output pointer to next char after parse completes (output)
  881. * @param dev_type parsed device type (output)
  882. * @param dev_num parsed device number (output)
  883. * @return 0 on success, 1 otherwise
  884. */
  885. int mtd_id_parse(const char *id, const char **ret_id, u8 *dev_type,
  886. u8 *dev_num)
  887. {
  888. const char *p = id;
  889. *dev_type = 0;
  890. if (strncmp(p, "nand", 4) == 0) {
  891. *dev_type = MTD_DEV_TYPE_NAND;
  892. p += 4;
  893. } else if (strncmp(p, "nor", 3) == 0) {
  894. *dev_type = MTD_DEV_TYPE_NOR;
  895. p += 3;
  896. } else if (strncmp(p, "onenand", 7) == 0) {
  897. *dev_type = MTD_DEV_TYPE_ONENAND;
  898. p += 7;
  899. } else {
  900. printf("incorrect device type in %s\n", id);
  901. return 1;
  902. }
  903. if (!isdigit(*p)) {
  904. printf("incorrect device number in %s\n", id);
  905. return 1;
  906. }
  907. *dev_num = simple_strtoul(p, (char **)&p, 0);
  908. if (ret_id)
  909. *ret_id = p;
  910. return 0;
  911. }
  912. /**
  913. * Process all devices and generate corresponding mtdparts string describing
  914. * all partitions on all devices.
  915. *
  916. * @param buf output buffer holding generated mtdparts string (output)
  917. * @param buflen buffer size
  918. * @return 0 on success, 1 otherwise
  919. */
  920. static int generate_mtdparts(char *buf, u32 buflen)
  921. {
  922. struct list_head *pentry, *dentry;
  923. struct mtd_device *dev;
  924. struct part_info *part, *prev_part;
  925. char *p = buf;
  926. char tmpbuf[32];
  927. u64 size, offset;
  928. u32 len, part_cnt;
  929. u32 maxlen = buflen - 1;
  930. debug("--- generate_mtdparts ---\n");
  931. if (list_empty(&devices)) {
  932. buf[0] = '\0';
  933. return 0;
  934. }
  935. strcpy(p, "mtdparts=");
  936. p += 9;
  937. list_for_each(dentry, &devices) {
  938. dev = list_entry(dentry, struct mtd_device, link);
  939. /* copy mtd_id */
  940. len = strlen(dev->id->mtd_id) + 1;
  941. if (len > maxlen)
  942. goto cleanup;
  943. memcpy(p, dev->id->mtd_id, len - 1);
  944. p += len - 1;
  945. *(p++) = ':';
  946. maxlen -= len;
  947. /* format partitions */
  948. prev_part = NULL;
  949. part_cnt = 0;
  950. list_for_each(pentry, &dev->parts) {
  951. part = list_entry(pentry, struct part_info, link);
  952. size = part->size;
  953. offset = part->offset;
  954. part_cnt++;
  955. /* partition size */
  956. memsize_format(tmpbuf, size);
  957. len = strlen(tmpbuf);
  958. if (len > maxlen)
  959. goto cleanup;
  960. memcpy(p, tmpbuf, len);
  961. p += len;
  962. maxlen -= len;
  963. /* add offset only when there is a gap between
  964. * partitions */
  965. if ((!prev_part && (offset != 0)) ||
  966. (prev_part && ((prev_part->offset + prev_part->size) != part->offset))) {
  967. memsize_format(tmpbuf, offset);
  968. len = strlen(tmpbuf) + 1;
  969. if (len > maxlen)
  970. goto cleanup;
  971. *(p++) = '@';
  972. memcpy(p, tmpbuf, len - 1);
  973. p += len - 1;
  974. maxlen -= len;
  975. }
  976. /* copy name only if user supplied */
  977. if(!part->auto_name) {
  978. len = strlen(part->name) + 2;
  979. if (len > maxlen)
  980. goto cleanup;
  981. *(p++) = '(';
  982. memcpy(p, part->name, len - 2);
  983. p += len - 2;
  984. *(p++) = ')';
  985. maxlen -= len;
  986. }
  987. /* ro mask flag */
  988. if (part->mask_flags && MTD_WRITEABLE_CMD) {
  989. len = 2;
  990. if (len > maxlen)
  991. goto cleanup;
  992. *(p++) = 'r';
  993. *(p++) = 'o';
  994. maxlen -= 2;
  995. }
  996. /* print ',' separator if there are other partitions
  997. * following */
  998. if (dev->num_parts > part_cnt) {
  999. if (1 > maxlen)
  1000. goto cleanup;
  1001. *(p++) = ',';
  1002. maxlen--;
  1003. }
  1004. prev_part = part;
  1005. }
  1006. /* print ';' separator if there are other devices following */
  1007. if (dentry->next != &devices) {
  1008. if (1 > maxlen)
  1009. goto cleanup;
  1010. *(p++) = ';';
  1011. maxlen--;
  1012. }
  1013. }
  1014. /* we still have at least one char left, as we decremented maxlen at
  1015. * the begining */
  1016. *p = '\0';
  1017. return 0;
  1018. cleanup:
  1019. last_parts[0] = '\0';
  1020. return 1;
  1021. }
  1022. /**
  1023. * Call generate_mtdparts to process all devices and generate corresponding
  1024. * mtdparts string, save it in mtdparts environment variable.
  1025. *
  1026. * @param buf output buffer holding generated mtdparts string (output)
  1027. * @param buflen buffer size
  1028. * @return 0 on success, 1 otherwise
  1029. */
  1030. static int generate_mtdparts_save(char *buf, u32 buflen)
  1031. {
  1032. int ret;
  1033. ret = generate_mtdparts(buf, buflen);
  1034. if ((buf[0] != '\0') && (ret == 0))
  1035. setenv("mtdparts", buf);
  1036. else
  1037. setenv("mtdparts", NULL);
  1038. return ret;
  1039. }
  1040. #if defined(CONFIG_CMD_MTDPARTS_SHOW_NET_SIZES)
  1041. /**
  1042. * Get the net size (w/o bad blocks) of the given partition.
  1043. *
  1044. * @param mtd the mtd info
  1045. * @param part the partition
  1046. * @return the calculated net size of this partition
  1047. */
  1048. static uint64_t net_part_size(struct mtd_info *mtd, struct part_info *part)
  1049. {
  1050. uint64_t i, net_size = 0;
  1051. if (!mtd->block_isbad)
  1052. return part->size;
  1053. for (i = 0; i < part->size; i += mtd->erasesize) {
  1054. if (!mtd->block_isbad(mtd, part->offset + i))
  1055. net_size += mtd->erasesize;
  1056. }
  1057. return net_size;
  1058. }
  1059. #endif
  1060. static void print_partition_table(void)
  1061. {
  1062. struct list_head *dentry, *pentry;
  1063. struct part_info *part;
  1064. struct mtd_device *dev;
  1065. int part_num;
  1066. list_for_each(dentry, &devices) {
  1067. dev = list_entry(dentry, struct mtd_device, link);
  1068. /* list partitions for given device */
  1069. part_num = 0;
  1070. #if defined(CONFIG_CMD_MTDPARTS_SHOW_NET_SIZES)
  1071. struct mtd_info *mtd;
  1072. if (get_mtd_info(dev->id->type, dev->id->num, &mtd))
  1073. return;
  1074. printf("\ndevice %s%d <%s>, # parts = %d\n",
  1075. MTD_DEV_TYPE(dev->id->type), dev->id->num,
  1076. dev->id->mtd_id, dev->num_parts);
  1077. printf(" #: name\t\tsize\t\tnet size\toffset\t\tmask_flags\n");
  1078. list_for_each(pentry, &dev->parts) {
  1079. u32 net_size;
  1080. char *size_note;
  1081. part = list_entry(pentry, struct part_info, link);
  1082. net_size = net_part_size(mtd, part);
  1083. size_note = part->size == net_size ? " " : " (!)";
  1084. printf("%2d: %-20s0x%08x\t0x%08x%s\t0x%08x\t%d\n",
  1085. part_num, part->name, part->size,
  1086. net_size, size_note, part->offset,
  1087. part->mask_flags);
  1088. #else /* !defined(CONFIG_CMD_MTDPARTS_SHOW_NET_SIZES) */
  1089. printf("\ndevice %s%d <%s>, # parts = %d\n",
  1090. MTD_DEV_TYPE(dev->id->type), dev->id->num,
  1091. dev->id->mtd_id, dev->num_parts);
  1092. printf(" #: name\t\tsize\t\toffset\t\tmask_flags\n");
  1093. list_for_each(pentry, &dev->parts) {
  1094. part = list_entry(pentry, struct part_info, link);
  1095. printf("%2d: %-20s0x%08llx\t0x%08llx\t%d\n",
  1096. part_num, part->name, part->size,
  1097. part->offset, part->mask_flags);
  1098. #endif /* defined(CONFIG_CMD_MTDPARTS_SHOW_NET_SIZES) */
  1099. part_num++;
  1100. }
  1101. }
  1102. if (list_empty(&devices))
  1103. printf("no partitions defined\n");
  1104. }
  1105. /**
  1106. * Format and print out a partition list for each device from global device
  1107. * list.
  1108. */
  1109. static void list_partitions(void)
  1110. {
  1111. struct part_info *part;
  1112. debug("\n---list_partitions---\n");
  1113. print_partition_table();
  1114. /* current_mtd_dev is not NULL only when we have non empty device list */
  1115. if (current_mtd_dev) {
  1116. part = mtd_part_info(current_mtd_dev, current_mtd_partnum);
  1117. if (part) {
  1118. printf("\nactive partition: %s%d,%d - (%s) 0x%08llx @ 0x%08llx\n",
  1119. MTD_DEV_TYPE(current_mtd_dev->id->type),
  1120. current_mtd_dev->id->num, current_mtd_partnum,
  1121. part->name, part->size, part->offset);
  1122. } else {
  1123. printf("could not get current partition info\n\n");
  1124. }
  1125. }
  1126. printf("\ndefaults:\n");
  1127. printf("mtdids : %s\n",
  1128. mtdids_default ? mtdids_default : "none");
  1129. /*
  1130. * Using printf() here results in printbuffer overflow
  1131. * if default mtdparts string is greater than console
  1132. * printbuffer. Use puts() to prevent system crashes.
  1133. */
  1134. puts("mtdparts: ");
  1135. puts(mtdparts_default ? mtdparts_default : "none");
  1136. puts("\n");
  1137. }
  1138. /**
  1139. * Given partition identifier in form of <dev_type><dev_num>,<part_num> find
  1140. * corresponding device and verify partition number.
  1141. *
  1142. * @param id string describing device and partition or partition name
  1143. * @param dev pointer to the requested device (output)
  1144. * @param part_num verified partition number (output)
  1145. * @param part pointer to requested partition (output)
  1146. * @return 0 on success, 1 otherwise
  1147. */
  1148. int find_dev_and_part(const char *id, struct mtd_device **dev,
  1149. u8 *part_num, struct part_info **part)
  1150. {
  1151. struct list_head *dentry, *pentry;
  1152. u8 type, dnum, pnum;
  1153. const char *p;
  1154. debug("--- find_dev_and_part ---\nid = %s\n", id);
  1155. list_for_each(dentry, &devices) {
  1156. *part_num = 0;
  1157. *dev = list_entry(dentry, struct mtd_device, link);
  1158. list_for_each(pentry, &(*dev)->parts) {
  1159. *part = list_entry(pentry, struct part_info, link);
  1160. if (strcmp((*part)->name, id) == 0)
  1161. return 0;
  1162. (*part_num)++;
  1163. }
  1164. }
  1165. p = id;
  1166. *dev = NULL;
  1167. *part = NULL;
  1168. *part_num = 0;
  1169. if (mtd_id_parse(p, &p, &type, &dnum) != 0)
  1170. return 1;
  1171. if ((*p++ != ',') || (*p == '\0')) {
  1172. printf("no partition number specified\n");
  1173. return 1;
  1174. }
  1175. pnum = simple_strtoul(p, (char **)&p, 0);
  1176. if (*p != '\0') {
  1177. printf("unexpected trailing character '%c'\n", *p);
  1178. return 1;
  1179. }
  1180. if ((*dev = device_find(type, dnum)) == NULL) {
  1181. printf("no such device %s%d\n", MTD_DEV_TYPE(type), dnum);
  1182. return 1;
  1183. }
  1184. if ((*part = mtd_part_info(*dev, pnum)) == NULL) {
  1185. printf("no such partition\n");
  1186. *dev = NULL;
  1187. return 1;
  1188. }
  1189. *part_num = pnum;
  1190. return 0;
  1191. }
  1192. /**
  1193. * Find and delete partition. For partition id format see find_dev_and_part().
  1194. *
  1195. * @param id string describing device and partition
  1196. * @return 0 on success, 1 otherwise
  1197. */
  1198. static int delete_partition(const char *id)
  1199. {
  1200. u8 pnum;
  1201. struct mtd_device *dev;
  1202. struct part_info *part;
  1203. if (find_dev_and_part(id, &dev, &pnum, &part) == 0) {
  1204. debug("delete_partition: device = %s%d, partition %d = (%s) 0x%08llx@0x%08llx\n",
  1205. MTD_DEV_TYPE(dev->id->type), dev->id->num, pnum,
  1206. part->name, part->size, part->offset);
  1207. if (part_del(dev, part) != 0)
  1208. return 1;
  1209. if (generate_mtdparts_save(last_parts, MTDPARTS_MAXLEN) != 0) {
  1210. printf("generated mtdparts too long, resetting to null\n");
  1211. return 1;
  1212. }
  1213. return 0;
  1214. }
  1215. printf("partition %s not found\n", id);
  1216. return 1;
  1217. }
  1218. #if defined(CONFIG_CMD_MTDPARTS_SPREAD)
  1219. /**
  1220. * Increase the size of the given partition so that it's net size is at least
  1221. * as large as the size member and such that the next partition would start on a
  1222. * good block if it were adjacent to this partition.
  1223. *
  1224. * @param mtd the mtd device
  1225. * @param part the partition
  1226. * @param next_offset pointer to the offset of the next partition after this
  1227. * partition's size has been modified (output)
  1228. */
  1229. static void spread_partition(struct mtd_info *mtd, struct part_info *part,
  1230. uint64_t *next_offset)
  1231. {
  1232. uint64_t net_size, padding_size = 0;
  1233. int truncated;
  1234. mtd_get_len_incl_bad(mtd, part->offset, part->size, &net_size,
  1235. &truncated);
  1236. /*
  1237. * Absorb bad blocks immediately following this
  1238. * partition also into the partition, such that
  1239. * the next partition starts with a good block.
  1240. */
  1241. if (!truncated) {
  1242. mtd_get_len_incl_bad(mtd, part->offset + net_size,
  1243. mtd->erasesize, &padding_size, &truncated);
  1244. if (truncated)
  1245. padding_size = 0;
  1246. else
  1247. padding_size -= mtd->erasesize;
  1248. }
  1249. if (truncated) {
  1250. printf("truncated partition %s to %lld bytes\n", part->name,
  1251. (uint64_t) net_size + padding_size);
  1252. }
  1253. part->size = net_size + padding_size;
  1254. *next_offset = part->offset + part->size;
  1255. }
  1256. /**
  1257. * Adjust all of the partition sizes, such that all partitions are at least
  1258. * as big as their mtdparts environment variable sizes and they each start
  1259. * on a good block.
  1260. *
  1261. * @return 0 on success, 1 otherwise
  1262. */
  1263. static int spread_partitions(void)
  1264. {
  1265. struct list_head *dentry, *pentry;
  1266. struct mtd_device *dev;
  1267. struct part_info *part;
  1268. struct mtd_info *mtd;
  1269. int part_num;
  1270. uint64_t cur_offs;
  1271. list_for_each(dentry, &devices) {
  1272. dev = list_entry(dentry, struct mtd_device, link);
  1273. if (get_mtd_info(dev->id->type, dev->id->num, &mtd))
  1274. return 1;
  1275. part_num = 0;
  1276. cur_offs = 0;
  1277. list_for_each(pentry, &dev->parts) {
  1278. part = list_entry(pentry, struct part_info, link);
  1279. debug("spread_partitions: device = %s%d, partition %d ="
  1280. " (%s) 0x%08x@0x%08x\n",
  1281. MTD_DEV_TYPE(dev->id->type), dev->id->num,
  1282. part_num, part->name, part->size,
  1283. part->offset);
  1284. if (cur_offs > part->offset)
  1285. part->offset = cur_offs;
  1286. spread_partition(mtd, part, &cur_offs);
  1287. part_num++;
  1288. }
  1289. }
  1290. index_partitions();
  1291. if (generate_mtdparts_save(last_parts, MTDPARTS_MAXLEN) != 0) {
  1292. printf("generated mtdparts too long, resetting to null\n");
  1293. return 1;
  1294. }
  1295. return 0;
  1296. }
  1297. #endif /* CONFIG_CMD_MTDPARTS_SPREAD */
  1298. /**
  1299. * Accept character string describing mtd partitions and call device_parse()
  1300. * for each entry. Add created devices to the global devices list.
  1301. *
  1302. * @param mtdparts string specifing mtd partitions
  1303. * @return 0 on success, 1 otherwise
  1304. */
  1305. static int parse_mtdparts(const char *const mtdparts)
  1306. {
  1307. const char *p = mtdparts;
  1308. struct mtd_device *dev;
  1309. int err = 1;
  1310. char tmp_parts[MTDPARTS_MAXLEN];
  1311. debug("\n---parse_mtdparts---\nmtdparts = %s\n\n", p);
  1312. /* delete all devices and partitions */
  1313. if (mtd_devices_init() != 0) {
  1314. printf("could not initialise device list\n");
  1315. return err;
  1316. }
  1317. /* re-read 'mtdparts' variable, mtd_devices_init may be updating env */
  1318. if (gd->flags & GD_FLG_ENV_READY) {
  1319. p = getenv("mtdparts");
  1320. } else {
  1321. p = tmp_parts;
  1322. getenv_f("mtdparts", tmp_parts, MTDPARTS_MAXLEN);
  1323. }
  1324. if (strncmp(p, "mtdparts=", 9) != 0) {
  1325. printf("mtdparts variable doesn't start with 'mtdparts='\n");
  1326. return err;
  1327. }
  1328. p += 9;
  1329. while (p && (*p != '\0')) {
  1330. err = 1;
  1331. if ((device_parse(p, &p, &dev) != 0) || (!dev))
  1332. break;
  1333. debug("+ device: %s\t%d\t%s\n", MTD_DEV_TYPE(dev->id->type),
  1334. dev->id->num, dev->id->mtd_id);
  1335. /* check if parsed device is already on the list */
  1336. if (device_find(dev->id->type, dev->id->num) != NULL) {
  1337. printf("device %s%d redefined, please correct mtdparts variable\n",
  1338. MTD_DEV_TYPE(dev->id->type), dev->id->num);
  1339. break;
  1340. }
  1341. list_add_tail(&dev->link, &devices);
  1342. err = 0;
  1343. }
  1344. if (err == 1) {
  1345. device_delall(&devices);
  1346. return 1;
  1347. }
  1348. return 0;
  1349. }
  1350. /**
  1351. * Parse provided string describing mtdids mapping (see file header for mtdids
  1352. * variable format). Allocate memory for each entry and add all found entries
  1353. * to the global mtdids list.
  1354. *
  1355. * @param ids mapping string
  1356. * @return 0 on success, 1 otherwise
  1357. */
  1358. static int parse_mtdids(const char *const ids)
  1359. {
  1360. const char *p = ids;
  1361. const char *mtd_id;
  1362. int mtd_id_len;
  1363. struct mtdids *id;
  1364. struct list_head *entry, *n;
  1365. struct mtdids *id_tmp;
  1366. u8 type, num;
  1367. u64 size;
  1368. int ret = 1;
  1369. debug("\n---parse_mtdids---\nmtdids = %s\n\n", ids);
  1370. /* clean global mtdids list */
  1371. list_for_each_safe(entry, n, &mtdids) {
  1372. id_tmp = list_entry(entry, struct mtdids, link);
  1373. debug("mtdids del: %d %d\n", id_tmp->type, id_tmp->num);
  1374. list_del(entry);
  1375. free(id_tmp);
  1376. }
  1377. last_ids[0] = '\0';
  1378. INIT_LIST_HEAD(&mtdids);
  1379. while(p && (*p != '\0')) {
  1380. ret = 1;
  1381. /* parse 'nor'|'nand'|'onenand'<dev-num> */
  1382. if (mtd_id_parse(p, &p, &type, &num) != 0)
  1383. break;
  1384. if (*p != '=') {
  1385. printf("mtdids: incorrect <dev-num>\n");
  1386. break;
  1387. }
  1388. p++;
  1389. /* check if requested device exists */
  1390. if (mtd_device_validate(type, num, &size) != 0)
  1391. return 1;
  1392. /* locate <mtd-id> */
  1393. mtd_id = p;
  1394. if ((p = strchr(mtd_id, ',')) != NULL) {
  1395. mtd_id_len = p - mtd_id + 1;
  1396. p++;
  1397. } else {
  1398. mtd_id_len = strlen(mtd_id) + 1;
  1399. }
  1400. if (mtd_id_len == 0) {
  1401. printf("mtdids: no <mtd-id> identifier\n");
  1402. break;
  1403. }
  1404. /* check if this id is already on the list */
  1405. int double_entry = 0;
  1406. list_for_each(entry, &mtdids) {
  1407. id_tmp = list_entry(entry, struct mtdids, link);
  1408. if ((id_tmp->type == type) && (id_tmp->num == num)) {
  1409. double_entry = 1;
  1410. break;
  1411. }
  1412. }
  1413. if (double_entry) {
  1414. printf("device id %s%d redefined, please correct mtdids variable\n",
  1415. MTD_DEV_TYPE(type), num);
  1416. break;
  1417. }
  1418. /* allocate mtdids structure */
  1419. if (!(id = (struct mtdids *)malloc(sizeof(struct mtdids) + mtd_id_len))) {
  1420. printf("out of memory\n");
  1421. break;
  1422. }
  1423. memset(id, 0, sizeof(struct mtdids) + mtd_id_len);
  1424. id->num = num;
  1425. id->type = type;
  1426. id->size = size;
  1427. id->mtd_id = (char *)(id + 1);
  1428. strncpy(id->mtd_id, mtd_id, mtd_id_len - 1);
  1429. id->mtd_id[mtd_id_len - 1] = '\0';
  1430. INIT_LIST_HEAD(&id->link);
  1431. debug("+ id %s%d\t%16lld bytes\t%s\n",
  1432. MTD_DEV_TYPE(id->type), id->num,
  1433. id->size, id->mtd_id);
  1434. list_add_tail(&id->link, &mtdids);
  1435. ret = 0;
  1436. }
  1437. if (ret == 1) {
  1438. /* clean mtdids list and free allocated memory */
  1439. list_for_each_safe(entry, n, &mtdids) {
  1440. id_tmp = list_entry(entry, struct mtdids, link);
  1441. list_del(entry);
  1442. free(id_tmp);
  1443. }
  1444. return 1;
  1445. }
  1446. return 0;
  1447. }
  1448. /**
  1449. * Parse and initialize global mtdids mapping and create global
  1450. * device/partition list.
  1451. *
  1452. * @return 0 on success, 1 otherwise
  1453. */
  1454. int mtdparts_init(void)
  1455. {
  1456. static int initialized = 0;
  1457. const char *ids, *parts;
  1458. const char *current_partition;
  1459. int ids_changed;
  1460. char tmp_ep[PARTITION_MAXLEN];
  1461. char tmp_parts[MTDPARTS_MAXLEN];
  1462. debug("\n---mtdparts_init---\n");
  1463. if (!initialized) {
  1464. INIT_LIST_HEAD(&mtdids);
  1465. INIT_LIST_HEAD(&devices);
  1466. memset(last_ids, 0, MTDIDS_MAXLEN);
  1467. memset(last_parts, 0, MTDPARTS_MAXLEN);
  1468. memset(last_partition, 0, PARTITION_MAXLEN);
  1469. initialized = 1;
  1470. }
  1471. /* get variables */
  1472. ids = getenv("mtdids");
  1473. /*
  1474. * The mtdparts variable tends to be long. If we need to access it
  1475. * before the env is relocated, then we need to use our own stack
  1476. * buffer. gd->env_buf will be too small.
  1477. */
  1478. if (gd->flags & GD_FLG_ENV_READY) {
  1479. parts = getenv("mtdparts");
  1480. } else {
  1481. parts = tmp_parts;
  1482. getenv_f("mtdparts", tmp_parts, MTDPARTS_MAXLEN);
  1483. }
  1484. current_partition = getenv("partition");
  1485. /* save it for later parsing, cannot rely on current partition pointer
  1486. * as 'partition' variable may be updated during init */
  1487. tmp_ep[0] = '\0';
  1488. if (current_partition)
  1489. strncpy(tmp_ep, current_partition, PARTITION_MAXLEN);
  1490. debug("last_ids : %s\n", last_ids);
  1491. debug("env_ids : %s\n", ids);
  1492. debug("last_parts: %s\n", last_parts);
  1493. debug("env_parts : %s\n\n", parts);
  1494. debug("last_partition : %s\n", last_partition);
  1495. debug("env_partition : %s\n", current_partition);
  1496. /* if mtdids variable is empty try to use defaults */
  1497. if (!ids) {
  1498. if (mtdids_default) {
  1499. debug("mtdids variable not defined, using default\n");
  1500. ids = mtdids_default;
  1501. setenv("mtdids", (char *)ids);
  1502. } else {
  1503. printf("mtdids not defined, no default present\n");
  1504. return 1;
  1505. }
  1506. }
  1507. if (strlen(ids) > MTDIDS_MAXLEN - 1) {
  1508. printf("mtdids too long (> %d)\n", MTDIDS_MAXLEN);
  1509. return 1;
  1510. }
  1511. /* do no try to use defaults when mtdparts variable is not defined,
  1512. * just check the length */
  1513. if (!parts)
  1514. printf("mtdparts variable not set, see 'help mtdparts'\n");
  1515. if (parts && (strlen(parts) > MTDPARTS_MAXLEN - 1)) {
  1516. printf("mtdparts too long (> %d)\n", MTDPARTS_MAXLEN);
  1517. return 1;
  1518. }
  1519. /* check if we have already parsed those mtdids */
  1520. if ((last_ids[0] != '\0') && (strcmp(last_ids, ids) == 0)) {
  1521. ids_changed = 0;
  1522. } else {
  1523. ids_changed = 1;
  1524. if (parse_mtdids(ids) != 0) {
  1525. mtd_devices_init();
  1526. return 1;
  1527. }
  1528. /* ok it's good, save new ids */
  1529. strncpy(last_ids, ids, MTDIDS_MAXLEN);
  1530. }
  1531. /* parse partitions if either mtdparts or mtdids were updated */
  1532. if (parts && ((last_parts[0] == '\0') || ((strcmp(last_parts, parts) != 0)) || ids_changed)) {
  1533. if (parse_mtdparts(parts) != 0)
  1534. return 1;
  1535. if (list_empty(&devices)) {
  1536. printf("mtdparts_init: no valid partitions\n");
  1537. return 1;
  1538. }
  1539. /* ok it's good, save new parts */
  1540. strncpy(last_parts, parts, MTDPARTS_MAXLEN);
  1541. /* reset first partition from first dev from the list as current */
  1542. current_mtd_dev = list_entry(devices.next, struct mtd_device, link);
  1543. current_mtd_partnum = 0;
  1544. current_save();
  1545. debug("mtdparts_init: current_mtd_dev = %s%d, current_mtd_partnum = %d\n",
  1546. MTD_DEV_TYPE(current_mtd_dev->id->type),
  1547. current_mtd_dev->id->num, current_mtd_partnum);
  1548. }
  1549. /* mtdparts variable was reset to NULL, delete all devices/partitions */
  1550. if (!parts && (last_parts[0] != '\0'))
  1551. return mtd_devices_init();
  1552. /* do not process current partition if mtdparts variable is null */
  1553. if (!parts)
  1554. return 0;
  1555. /* is current partition set in environment? if so, use it */
  1556. if ((tmp_ep[0] != '\0') && (strcmp(tmp_ep, last_partition) != 0)) {
  1557. struct part_info *p;
  1558. struct mtd_device *cdev;
  1559. u8 pnum;
  1560. debug("--- getting current partition: %s\n", tmp_ep);
  1561. if (find_dev_and_part(tmp_ep, &cdev, &pnum, &p) == 0) {
  1562. current_mtd_dev = cdev;
  1563. current_mtd_partnum = pnum;
  1564. current_save();
  1565. }
  1566. } else if (getenv("partition") == NULL) {
  1567. debug("no partition variable set, setting...\n");
  1568. current_save();
  1569. }
  1570. return 0;
  1571. }
  1572. /**
  1573. * Return pointer to the partition of a requested number from a requested
  1574. * device.
  1575. *
  1576. * @param dev device that is to be searched for a partition
  1577. * @param part_num requested partition number
  1578. * @return pointer to the part_info, NULL otherwise
  1579. */
  1580. static struct part_info* mtd_part_info(struct mtd_device *dev, unsigned int part_num)
  1581. {
  1582. struct list_head *entry;
  1583. struct part_info *part;
  1584. int num;
  1585. if (!dev)
  1586. return NULL;
  1587. debug("\n--- mtd_part_info: partition number %d for device %s%d (%s)\n",
  1588. part_num, MTD_DEV_TYPE(dev->id->type),
  1589. dev->id->num, dev->id->mtd_id);
  1590. if (part_num >= dev->num_parts) {
  1591. printf("invalid partition number %d for device %s%d (%s)\n",
  1592. part_num, MTD_DEV_TYPE(dev->id->type),
  1593. dev->id->num, dev->id->mtd_id);
  1594. return NULL;
  1595. }
  1596. /* locate partition number, return it */
  1597. num = 0;
  1598. list_for_each(entry, &dev->parts) {
  1599. part = list_entry(entry, struct part_info, link);
  1600. if (part_num == num++) {
  1601. return part;
  1602. }
  1603. }
  1604. return NULL;
  1605. }
  1606. /***************************************************/
  1607. /* U-Boot commands */
  1608. /***************************************************/
  1609. /* command line only */
  1610. /**
  1611. * Routine implementing u-boot chpart command. Sets new current partition based
  1612. * on the user supplied partition id. For partition id format see find_dev_and_part().
  1613. *
  1614. * @param cmdtp command internal data
  1615. * @param flag command flag
  1616. * @param argc number of arguments supplied to the command
  1617. * @param argv arguments list
  1618. * @return 0 on success, 1 otherwise
  1619. */
  1620. static int do_chpart(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  1621. {
  1622. /* command line only */
  1623. struct mtd_device *dev;
  1624. struct part_info *part;
  1625. u8 pnum;
  1626. if (mtdparts_init() !=0)
  1627. return 1;
  1628. if (argc < 2) {
  1629. printf("no partition id specified\n");
  1630. return 1;
  1631. }
  1632. if (find_dev_and_part(argv[1], &dev, &pnum, &part) != 0)
  1633. return 1;
  1634. current_mtd_dev = dev;
  1635. current_mtd_partnum = pnum;
  1636. current_save();
  1637. printf("partition changed to %s%d,%d\n",
  1638. MTD_DEV_TYPE(dev->id->type), dev->id->num, pnum);
  1639. return 0;
  1640. }
  1641. /**
  1642. * Routine implementing u-boot mtdparts command. Initialize/update default global
  1643. * partition list and process user partition request (list, add, del).
  1644. *
  1645. * @param cmdtp command internal data
  1646. * @param flag command flag
  1647. * @param argc number of arguments supplied to the command
  1648. * @param argv arguments list
  1649. * @return 0 on success, 1 otherwise
  1650. */
  1651. static int do_mtdparts(cmd_tbl_t *cmdtp, int flag, int argc,
  1652. char * const argv[])
  1653. {
  1654. if (argc == 2) {
  1655. if (strcmp(argv[1], "default") == 0) {
  1656. setenv("mtdids", (char *)mtdids_default);
  1657. setenv("mtdparts", (char *)mtdparts_default);
  1658. setenv("partition", NULL);
  1659. mtdparts_init();
  1660. return 0;
  1661. } else if (strcmp(argv[1], "delall") == 0) {
  1662. /* this may be the first run, initialize lists if needed */
  1663. mtdparts_init();
  1664. setenv("mtdparts", NULL);
  1665. /* mtd_devices_init() calls current_save() */
  1666. return mtd_devices_init();
  1667. }
  1668. }
  1669. /* make sure we are in sync with env variables */
  1670. if (mtdparts_init() != 0)
  1671. return 1;
  1672. if (argc == 1) {
  1673. list_partitions();
  1674. return 0;
  1675. }
  1676. /* mtdparts add <mtd-dev> <size>[@<offset>] <name> [ro] */
  1677. if (((argc == 5) || (argc == 6)) && (strncmp(argv[1], "add", 3) == 0)) {
  1678. #define PART_ADD_DESC_MAXLEN 64
  1679. char tmpbuf[PART_ADD_DESC_MAXLEN];
  1680. #if defined(CONFIG_CMD_MTDPARTS_SPREAD)
  1681. struct mtd_info *mtd;
  1682. uint64_t next_offset;
  1683. #endif
  1684. u8 type, num, len;
  1685. struct mtd_device *dev;
  1686. struct mtd_device *dev_tmp;
  1687. struct mtdids *id;
  1688. struct part_info *p;
  1689. if (mtd_id_parse(argv[2], NULL, &type, &num) != 0)
  1690. return 1;
  1691. if ((id = id_find(type, num)) == NULL) {
  1692. printf("no such device %s defined in mtdids variable\n", argv[2]);
  1693. return 1;
  1694. }
  1695. len = strlen(id->mtd_id) + 1; /* 'mtd_id:' */
  1696. len += strlen(argv[3]); /* size@offset */
  1697. len += strlen(argv[4]) + 2; /* '(' name ')' */
  1698. if (argv[5] && (strlen(argv[5]) == 2))
  1699. len += 2; /* 'ro' */
  1700. if (len >= PART_ADD_DESC_MAXLEN) {
  1701. printf("too long partition description\n");
  1702. return 1;
  1703. }
  1704. sprintf(tmpbuf, "%s:%s(%s)%s",
  1705. id->mtd_id, argv[3], argv[4], argv[5] ? argv[5] : "");
  1706. debug("add tmpbuf: %s\n", tmpbuf);
  1707. if ((device_parse(tmpbuf, NULL, &dev) != 0) || (!dev))
  1708. return 1;
  1709. debug("+ %s\t%d\t%s\n", MTD_DEV_TYPE(dev->id->type),
  1710. dev->id->num, dev->id->mtd_id);
  1711. p = list_entry(dev->parts.next, struct part_info, link);
  1712. #if defined(CONFIG_CMD_MTDPARTS_SPREAD)
  1713. if (get_mtd_info(dev->id->type, dev->id->num, &mtd))
  1714. return 1;
  1715. if (!strcmp(&argv[1][3], ".spread")) {
  1716. spread_partition(mtd, p, &next_offset);
  1717. debug("increased %s to %d bytes\n", p->name, p->size);
  1718. }
  1719. #endif
  1720. dev_tmp = device_find(dev->id->type, dev->id->num);
  1721. if (dev_tmp == NULL) {
  1722. device_add(dev);
  1723. } else if (part_add(dev_tmp, p) != 0) {
  1724. /* merge new partition with existing ones*/
  1725. device_del(dev);
  1726. return 1;
  1727. }
  1728. if (generate_mtdparts_save(last_parts, MTDPARTS_MAXLEN) != 0) {
  1729. printf("generated mtdparts too long, resetting to null\n");
  1730. return 1;
  1731. }
  1732. return 0;
  1733. }
  1734. /* mtdparts del part-id */
  1735. if ((argc == 3) && (strcmp(argv[1], "del") == 0)) {
  1736. debug("del: part-id = %s\n", argv[2]);
  1737. return delete_partition(argv[2]);
  1738. }
  1739. #if defined(CONFIG_CMD_MTDPARTS_SPREAD)
  1740. if ((argc == 2) && (strcmp(argv[1], "spread") == 0))
  1741. return spread_partitions();
  1742. #endif /* CONFIG_CMD_MTDPARTS_SPREAD */
  1743. return CMD_RET_USAGE;
  1744. }
  1745. /***************************************************/
  1746. U_BOOT_CMD(
  1747. chpart, 2, 0, do_chpart,
  1748. "change active partition",
  1749. "part-id\n"
  1750. " - change active partition (e.g. part-id = nand0,1)"
  1751. );
  1752. #ifdef CONFIG_SYS_LONGHELP
  1753. static char mtdparts_help_text[] =
  1754. "\n"
  1755. " - list partition table\n"
  1756. "mtdparts delall\n"
  1757. " - delete all partitions\n"
  1758. "mtdparts del part-id\n"
  1759. " - delete partition (e.g. part-id = nand0,1)\n"
  1760. "mtdparts add <mtd-dev> <size>[@<offset>] [<name>] [ro]\n"
  1761. " - add partition\n"
  1762. #if defined(CONFIG_CMD_MTDPARTS_SPREAD)
  1763. "mtdparts add.spread <mtd-dev> <size>[@<offset>] [<name>] [ro]\n"
  1764. " - add partition, padding size by skipping bad blocks\n"
  1765. #endif
  1766. "mtdparts default\n"
  1767. " - reset partition table to defaults\n"
  1768. #if defined(CONFIG_CMD_MTDPARTS_SPREAD)
  1769. "mtdparts spread\n"
  1770. " - adjust the sizes of the partitions so they are\n"
  1771. " at least as big as the mtdparts variable specifies\n"
  1772. " and they each start on a good block\n\n"
  1773. #else
  1774. "\n"
  1775. #endif /* CONFIG_CMD_MTDPARTS_SPREAD */
  1776. "-----\n\n"
  1777. "this command uses three environment variables:\n\n"
  1778. "'partition' - keeps current partition identifier\n\n"
  1779. "partition := <part-id>\n"
  1780. "<part-id> := <dev-id>,part_num\n\n"
  1781. "'mtdids' - linux kernel mtd device id <-> u-boot device id mapping\n\n"
  1782. "mtdids=<idmap>[,<idmap>,...]\n\n"
  1783. "<idmap> := <dev-id>=<mtd-id>\n"
  1784. "<dev-id> := 'nand'|'nor'|'onenand'<dev-num>\n"
  1785. "<dev-num> := mtd device number, 0...\n"
  1786. "<mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)\n\n"
  1787. "'mtdparts' - partition list\n\n"
  1788. "mtdparts=mtdparts=<mtd-def>[;<mtd-def>...]\n\n"
  1789. "<mtd-def> := <mtd-id>:<part-def>[,<part-def>...]\n"
  1790. "<mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)\n"
  1791. "<part-def> := <size>[@<offset>][<name>][<ro-flag>]\n"
  1792. "<size> := standard linux memsize OR '-' to denote all remaining space\n"
  1793. "<offset> := partition start offset within the device\n"
  1794. "<name> := '(' NAME ')'\n"
  1795. "<ro-flag> := when set to 'ro' makes partition read-only (not used, passed to kernel)";
  1796. #endif
  1797. U_BOOT_CMD(
  1798. mtdparts, 6, 0, do_mtdparts,
  1799. "define flash/nand partitions", mtdparts_help_text
  1800. );
  1801. /***************************************************/