cmd_mtdparts.c 52 KB

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