mtdparts.c 53 KB

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