mtdparts.c 53 KB

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