mtdparts.c 53 KB

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