cmd_mtdparts.c 53 KB

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