mtdparts.c 53 KB

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