mtdparts.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131
  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. pend = strchr(p, ';');
  757. debug("dev type = %d (%s), dev num = %d, mtd-id = %s\n",
  758. id->type, MTD_DEV_TYPE(id->type),
  759. id->num, id->mtd_id);
  760. debug("parsing partitions %.*s\n", (int)(pend ? pend - p : strlen(p)), p);
  761. /* parse partitions */
  762. num_parts = 0;
  763. offset = 0;
  764. if ((dev = device_find(id->type, id->num)) != NULL) {
  765. /* if device already exists start at the end of the last partition */
  766. part = list_entry(dev->parts.prev, struct part_info, link);
  767. offset = part->offset + part->size;
  768. }
  769. while (p && (*p != '\0') && (*p != ';')) {
  770. err = 1;
  771. if ((part_parse(p, &p, &part) != 0) || (!part))
  772. break;
  773. /* calculate offset when not specified */
  774. if (part->offset == OFFSET_NOT_SPECIFIED)
  775. part->offset = offset;
  776. else
  777. offset = part->offset;
  778. /* verify alignment and size */
  779. if (part_validate(id, part) != 0)
  780. break;
  781. offset += part->size;
  782. /* partition is ok, add it to the list */
  783. list_add_tail(&part->link, &tmp_list);
  784. num_parts++;
  785. err = 0;
  786. }
  787. if (err == 1) {
  788. part_delall(&tmp_list);
  789. return 1;
  790. }
  791. debug("\ntotal partitions: %d\n", num_parts);
  792. /* check for next device presence */
  793. if (p) {
  794. if (*p == ';') {
  795. if (ret)
  796. *ret = ++p;
  797. } else if (*p == '\0') {
  798. if (ret)
  799. *ret = p;
  800. } else {
  801. printf("unexpected character '%c' at the end of device\n", *p);
  802. if (ret)
  803. *ret = NULL;
  804. return 1;
  805. }
  806. }
  807. /* allocate memory for mtd_device structure */
  808. if ((dev = (struct mtd_device *)malloc(sizeof(struct mtd_device))) == NULL) {
  809. printf("out of memory\n");
  810. return 1;
  811. }
  812. memset(dev, 0, sizeof(struct mtd_device));
  813. dev->id = id;
  814. dev->num_parts = 0; /* part_sort_add increments num_parts */
  815. INIT_LIST_HEAD(&dev->parts);
  816. INIT_LIST_HEAD(&dev->link);
  817. /* move partitions from tmp_list to dev->parts */
  818. list_for_each_safe(entry, n, &tmp_list) {
  819. part = list_entry(entry, struct part_info, link);
  820. list_del(entry);
  821. if (part_sort_add(dev, part) != 0) {
  822. device_del(dev);
  823. return 1;
  824. }
  825. }
  826. *retdev = dev;
  827. debug("===\n\n");
  828. return 0;
  829. }
  830. /**
  831. * Initialize global device list.
  832. *
  833. * @return 0 on success, 1 otherwise
  834. */
  835. static int mtd_devices_init(void)
  836. {
  837. last_parts[0] = '\0';
  838. current_mtd_dev = NULL;
  839. current_save();
  840. return device_delall(&devices);
  841. }
  842. /*
  843. * Search global mtdids list and find id of requested type and number.
  844. *
  845. * @return pointer to the id if it exists, NULL otherwise
  846. */
  847. static struct mtdids* id_find(u8 type, u8 num)
  848. {
  849. struct list_head *entry;
  850. struct mtdids *id;
  851. list_for_each(entry, &mtdids) {
  852. id = list_entry(entry, struct mtdids, link);
  853. if ((id->type == type) && (id->num == num))
  854. return id;
  855. }
  856. return NULL;
  857. }
  858. /**
  859. * Search global mtdids list and find id of a requested mtd_id.
  860. *
  861. * Note: first argument is not null terminated.
  862. *
  863. * @param mtd_id string containing requested mtd_id
  864. * @param mtd_id_len length of supplied mtd_id
  865. * @return pointer to the id if it exists, NULL otherwise
  866. */
  867. static struct mtdids* id_find_by_mtd_id(const char *mtd_id, unsigned int mtd_id_len)
  868. {
  869. struct list_head *entry;
  870. struct mtdids *id;
  871. debug("--- id_find_by_mtd_id: '%.*s' (len = %d)\n",
  872. mtd_id_len, mtd_id, mtd_id_len);
  873. list_for_each(entry, &mtdids) {
  874. id = list_entry(entry, struct mtdids, link);
  875. debug("entry: '%s' (len = %zu)\n",
  876. id->mtd_id, strlen(id->mtd_id));
  877. if (mtd_id_len != strlen(id->mtd_id))
  878. continue;
  879. if (strncmp(id->mtd_id, mtd_id, mtd_id_len) == 0)
  880. return id;
  881. }
  882. return NULL;
  883. }
  884. /**
  885. * Parse device id string <dev-id> := 'nand'|'nor'|'onenand'<dev-num>,
  886. * return device type and number.
  887. *
  888. * @param id string describing device id
  889. * @param ret_id output pointer to next char after parse completes (output)
  890. * @param dev_type parsed device type (output)
  891. * @param dev_num parsed device number (output)
  892. * @return 0 on success, 1 otherwise
  893. */
  894. int mtd_id_parse(const char *id, const char **ret_id, u8 *dev_type,
  895. u8 *dev_num)
  896. {
  897. const char *p = id;
  898. *dev_type = 0;
  899. if (strncmp(p, "nand", 4) == 0) {
  900. *dev_type = MTD_DEV_TYPE_NAND;
  901. p += 4;
  902. } else if (strncmp(p, "nor", 3) == 0) {
  903. *dev_type = MTD_DEV_TYPE_NOR;
  904. p += 3;
  905. } else if (strncmp(p, "onenand", 7) == 0) {
  906. *dev_type = MTD_DEV_TYPE_ONENAND;
  907. p += 7;
  908. } else {
  909. printf("incorrect device type in %s\n", id);
  910. return 1;
  911. }
  912. if (!isdigit(*p)) {
  913. printf("incorrect device number in %s\n", id);
  914. return 1;
  915. }
  916. *dev_num = simple_strtoul(p, (char **)&p, 0);
  917. if (ret_id)
  918. *ret_id = p;
  919. return 0;
  920. }
  921. /**
  922. * Process all devices and generate corresponding mtdparts string describing
  923. * all partitions on all devices.
  924. *
  925. * @param buf output buffer holding generated mtdparts string (output)
  926. * @param buflen buffer size
  927. * @return 0 on success, 1 otherwise
  928. */
  929. static int generate_mtdparts(char *buf, u32 buflen)
  930. {
  931. struct list_head *pentry, *dentry;
  932. struct mtd_device *dev;
  933. struct part_info *part, *prev_part;
  934. char *p = buf;
  935. char tmpbuf[32];
  936. u64 size, offset;
  937. u32 len, part_cnt;
  938. u32 maxlen = buflen - 1;
  939. debug("--- generate_mtdparts ---\n");
  940. if (list_empty(&devices)) {
  941. buf[0] = '\0';
  942. return 0;
  943. }
  944. strcpy(p, "mtdparts=");
  945. p += 9;
  946. list_for_each(dentry, &devices) {
  947. dev = list_entry(dentry, struct mtd_device, link);
  948. /* copy mtd_id */
  949. len = strlen(dev->id->mtd_id) + 1;
  950. if (len > maxlen)
  951. goto cleanup;
  952. memcpy(p, dev->id->mtd_id, len - 1);
  953. p += len - 1;
  954. *(p++) = ':';
  955. maxlen -= len;
  956. /* format partitions */
  957. prev_part = NULL;
  958. part_cnt = 0;
  959. list_for_each(pentry, &dev->parts) {
  960. part = list_entry(pentry, struct part_info, link);
  961. size = part->size;
  962. offset = part->offset;
  963. part_cnt++;
  964. /* partition size */
  965. memsize_format(tmpbuf, size);
  966. len = strlen(tmpbuf);
  967. if (len > maxlen)
  968. goto cleanup;
  969. memcpy(p, tmpbuf, len);
  970. p += len;
  971. maxlen -= len;
  972. /* add offset only when there is a gap between
  973. * partitions */
  974. if ((!prev_part && (offset != 0)) ||
  975. (prev_part && ((prev_part->offset + prev_part->size) != part->offset))) {
  976. memsize_format(tmpbuf, offset);
  977. len = strlen(tmpbuf) + 1;
  978. if (len > maxlen)
  979. goto cleanup;
  980. *(p++) = '@';
  981. memcpy(p, tmpbuf, len - 1);
  982. p += len - 1;
  983. maxlen -= len;
  984. }
  985. /* copy name only if user supplied */
  986. if(!part->auto_name) {
  987. len = strlen(part->name) + 2;
  988. if (len > maxlen)
  989. goto cleanup;
  990. *(p++) = '(';
  991. memcpy(p, part->name, len - 2);
  992. p += len - 2;
  993. *(p++) = ')';
  994. maxlen -= len;
  995. }
  996. /* ro mask flag */
  997. if (part->mask_flags && MTD_WRITEABLE_CMD) {
  998. len = 2;
  999. if (len > maxlen)
  1000. goto cleanup;
  1001. *(p++) = 'r';
  1002. *(p++) = 'o';
  1003. maxlen -= 2;
  1004. }
  1005. /* print ',' separator if there are other partitions
  1006. * following */
  1007. if (dev->num_parts > part_cnt) {
  1008. if (1 > maxlen)
  1009. goto cleanup;
  1010. *(p++) = ',';
  1011. maxlen--;
  1012. }
  1013. prev_part = part;
  1014. }
  1015. /* print ';' separator if there are other devices following */
  1016. if (dentry->next != &devices) {
  1017. if (1 > maxlen)
  1018. goto cleanup;
  1019. *(p++) = ';';
  1020. maxlen--;
  1021. }
  1022. }
  1023. /* we still have at least one char left, as we decremented maxlen at
  1024. * the begining */
  1025. *p = '\0';
  1026. return 0;
  1027. cleanup:
  1028. last_parts[0] = '\0';
  1029. return 1;
  1030. }
  1031. /**
  1032. * Call generate_mtdparts to process all devices and generate corresponding
  1033. * mtdparts string, save it in mtdparts environment variable.
  1034. *
  1035. * @param buf output buffer holding generated mtdparts string (output)
  1036. * @param buflen buffer size
  1037. * @return 0 on success, 1 otherwise
  1038. */
  1039. static int generate_mtdparts_save(char *buf, u32 buflen)
  1040. {
  1041. int ret;
  1042. ret = generate_mtdparts(buf, buflen);
  1043. if ((buf[0] != '\0') && (ret == 0))
  1044. env_set("mtdparts", buf);
  1045. else
  1046. env_set("mtdparts", NULL);
  1047. return ret;
  1048. }
  1049. #if defined(CONFIG_CMD_MTDPARTS_SHOW_NET_SIZES)
  1050. /**
  1051. * Get the net size (w/o bad blocks) of the given partition.
  1052. *
  1053. * @param mtd the mtd info
  1054. * @param part the partition
  1055. * @return the calculated net size of this partition
  1056. */
  1057. static uint64_t net_part_size(struct mtd_info *mtd, struct part_info *part)
  1058. {
  1059. uint64_t i, net_size = 0;
  1060. if (!mtd->block_isbad)
  1061. return part->size;
  1062. for (i = 0; i < part->size; i += mtd->erasesize) {
  1063. if (!mtd->block_isbad(mtd, part->offset + i))
  1064. net_size += mtd->erasesize;
  1065. }
  1066. return net_size;
  1067. }
  1068. #endif
  1069. static void print_partition_table(void)
  1070. {
  1071. struct list_head *dentry, *pentry;
  1072. struct part_info *part;
  1073. struct mtd_device *dev;
  1074. int part_num;
  1075. list_for_each(dentry, &devices) {
  1076. dev = list_entry(dentry, struct mtd_device, link);
  1077. /* list partitions for given device */
  1078. part_num = 0;
  1079. #if defined(CONFIG_CMD_MTDPARTS_SHOW_NET_SIZES)
  1080. struct mtd_info *mtd;
  1081. if (get_mtd_info(dev->id->type, dev->id->num, &mtd))
  1082. return;
  1083. printf("\ndevice %s%d <%s>, # parts = %d\n",
  1084. MTD_DEV_TYPE(dev->id->type), dev->id->num,
  1085. dev->id->mtd_id, dev->num_parts);
  1086. printf(" #: name\t\tsize\t\tnet size\toffset\t\tmask_flags\n");
  1087. list_for_each(pentry, &dev->parts) {
  1088. u32 net_size;
  1089. char *size_note;
  1090. part = list_entry(pentry, struct part_info, link);
  1091. net_size = net_part_size(mtd, part);
  1092. size_note = part->size == net_size ? " " : " (!)";
  1093. printf("%2d: %-20s0x%08x\t0x%08x%s\t0x%08x\t%d\n",
  1094. part_num, part->name, part->size,
  1095. net_size, size_note, part->offset,
  1096. part->mask_flags);
  1097. #else /* !defined(CONFIG_CMD_MTDPARTS_SHOW_NET_SIZES) */
  1098. printf("\ndevice %s%d <%s>, # parts = %d\n",
  1099. MTD_DEV_TYPE(dev->id->type), dev->id->num,
  1100. dev->id->mtd_id, dev->num_parts);
  1101. printf(" #: name\t\tsize\t\toffset\t\tmask_flags\n");
  1102. list_for_each(pentry, &dev->parts) {
  1103. part = list_entry(pentry, struct part_info, link);
  1104. printf("%2d: %-20s0x%08llx\t0x%08llx\t%d\n",
  1105. part_num, part->name, part->size,
  1106. part->offset, part->mask_flags);
  1107. #endif /* defined(CONFIG_CMD_MTDPARTS_SHOW_NET_SIZES) */
  1108. part_num++;
  1109. }
  1110. }
  1111. if (list_empty(&devices))
  1112. printf("no partitions defined\n");
  1113. }
  1114. /**
  1115. * Format and print out a partition list for each device from global device
  1116. * list.
  1117. */
  1118. static void list_partitions(void)
  1119. {
  1120. struct part_info *part;
  1121. debug("\n---list_partitions---\n");
  1122. print_partition_table();
  1123. /* current_mtd_dev is not NULL only when we have non empty device list */
  1124. if (current_mtd_dev) {
  1125. part = mtd_part_info(current_mtd_dev, current_mtd_partnum);
  1126. if (part) {
  1127. printf("\nactive partition: %s%d,%d - (%s) 0x%08llx @ 0x%08llx\n",
  1128. MTD_DEV_TYPE(current_mtd_dev->id->type),
  1129. current_mtd_dev->id->num, current_mtd_partnum,
  1130. part->name, part->size, part->offset);
  1131. } else {
  1132. printf("could not get current partition info\n\n");
  1133. }
  1134. }
  1135. printf("\ndefaults:\n");
  1136. printf("mtdids : %s\n",
  1137. mtdids_default ? mtdids_default : "none");
  1138. /*
  1139. * Using printf() here results in printbuffer overflow
  1140. * if default mtdparts string is greater than console
  1141. * printbuffer. Use puts() to prevent system crashes.
  1142. */
  1143. puts("mtdparts: ");
  1144. puts(mtdparts_default ? mtdparts_default : "none");
  1145. puts("\n");
  1146. }
  1147. /**
  1148. * Given partition identifier in form of <dev_type><dev_num>,<part_num> find
  1149. * corresponding device and verify partition number.
  1150. *
  1151. * @param id string describing device and partition or partition name
  1152. * @param dev pointer to the requested device (output)
  1153. * @param part_num verified partition number (output)
  1154. * @param part pointer to requested partition (output)
  1155. * @return 0 on success, 1 otherwise
  1156. */
  1157. int find_dev_and_part(const char *id, struct mtd_device **dev,
  1158. u8 *part_num, struct part_info **part)
  1159. {
  1160. struct list_head *dentry, *pentry;
  1161. u8 type, dnum, pnum;
  1162. const char *p;
  1163. debug("--- find_dev_and_part ---\nid = %s\n", id);
  1164. list_for_each(dentry, &devices) {
  1165. *part_num = 0;
  1166. *dev = list_entry(dentry, struct mtd_device, link);
  1167. list_for_each(pentry, &(*dev)->parts) {
  1168. *part = list_entry(pentry, struct part_info, link);
  1169. if (strcmp((*part)->name, id) == 0)
  1170. return 0;
  1171. (*part_num)++;
  1172. }
  1173. }
  1174. p = id;
  1175. *dev = NULL;
  1176. *part = NULL;
  1177. *part_num = 0;
  1178. if (mtd_id_parse(p, &p, &type, &dnum) != 0)
  1179. return 1;
  1180. if ((*p++ != ',') || (*p == '\0')) {
  1181. printf("no partition number specified\n");
  1182. return 1;
  1183. }
  1184. pnum = simple_strtoul(p, (char **)&p, 0);
  1185. if (*p != '\0') {
  1186. printf("unexpected trailing character '%c'\n", *p);
  1187. return 1;
  1188. }
  1189. if ((*dev = device_find(type, dnum)) == NULL) {
  1190. printf("no such device %s%d\n", MTD_DEV_TYPE(type), dnum);
  1191. return 1;
  1192. }
  1193. if ((*part = mtd_part_info(*dev, pnum)) == NULL) {
  1194. printf("no such partition\n");
  1195. *dev = NULL;
  1196. return 1;
  1197. }
  1198. *part_num = pnum;
  1199. return 0;
  1200. }
  1201. /**
  1202. * Find and delete partition. For partition id format see find_dev_and_part().
  1203. *
  1204. * @param id string describing device and partition
  1205. * @return 0 on success, 1 otherwise
  1206. */
  1207. static int delete_partition(const char *id)
  1208. {
  1209. u8 pnum;
  1210. struct mtd_device *dev;
  1211. struct part_info *part;
  1212. if (find_dev_and_part(id, &dev, &pnum, &part) == 0) {
  1213. debug("delete_partition: device = %s%d, partition %d = (%s) 0x%08llx@0x%08llx\n",
  1214. MTD_DEV_TYPE(dev->id->type), dev->id->num, pnum,
  1215. part->name, part->size, part->offset);
  1216. if (part_del(dev, part) != 0)
  1217. return 1;
  1218. if (generate_mtdparts_save(last_parts, MTDPARTS_MAXLEN) != 0) {
  1219. printf("generated mtdparts too long, resetting to null\n");
  1220. return 1;
  1221. }
  1222. return 0;
  1223. }
  1224. printf("partition %s not found\n", id);
  1225. return 1;
  1226. }
  1227. #if defined(CONFIG_CMD_MTDPARTS_SPREAD)
  1228. /**
  1229. * Increase the size of the given partition so that it's net size is at least
  1230. * as large as the size member and such that the next partition would start on a
  1231. * good block if it were adjacent to this partition.
  1232. *
  1233. * @param mtd the mtd device
  1234. * @param part the partition
  1235. * @param next_offset pointer to the offset of the next partition after this
  1236. * partition's size has been modified (output)
  1237. */
  1238. static void spread_partition(struct mtd_info *mtd, struct part_info *part,
  1239. uint64_t *next_offset)
  1240. {
  1241. uint64_t net_size, padding_size = 0;
  1242. int truncated;
  1243. mtd_get_len_incl_bad(mtd, part->offset, part->size, &net_size,
  1244. &truncated);
  1245. /*
  1246. * Absorb bad blocks immediately following this
  1247. * partition also into the partition, such that
  1248. * the next partition starts with a good block.
  1249. */
  1250. if (!truncated) {
  1251. mtd_get_len_incl_bad(mtd, part->offset + net_size,
  1252. mtd->erasesize, &padding_size, &truncated);
  1253. if (truncated)
  1254. padding_size = 0;
  1255. else
  1256. padding_size -= mtd->erasesize;
  1257. }
  1258. if (truncated) {
  1259. printf("truncated partition %s to %lld bytes\n", part->name,
  1260. (uint64_t) net_size + padding_size);
  1261. }
  1262. part->size = net_size + padding_size;
  1263. *next_offset = part->offset + part->size;
  1264. }
  1265. /**
  1266. * Adjust all of the partition sizes, such that all partitions are at least
  1267. * as big as their mtdparts environment variable sizes and they each start
  1268. * on a good block.
  1269. *
  1270. * @return 0 on success, 1 otherwise
  1271. */
  1272. static int spread_partitions(void)
  1273. {
  1274. struct list_head *dentry, *pentry;
  1275. struct mtd_device *dev;
  1276. struct part_info *part;
  1277. struct mtd_info *mtd;
  1278. int part_num;
  1279. uint64_t cur_offs;
  1280. list_for_each(dentry, &devices) {
  1281. dev = list_entry(dentry, struct mtd_device, link);
  1282. if (get_mtd_info(dev->id->type, dev->id->num, &mtd))
  1283. return 1;
  1284. part_num = 0;
  1285. cur_offs = 0;
  1286. list_for_each(pentry, &dev->parts) {
  1287. part = list_entry(pentry, struct part_info, link);
  1288. debug("spread_partitions: device = %s%d, partition %d ="
  1289. " (%s) 0x%08llx@0x%08llx\n",
  1290. MTD_DEV_TYPE(dev->id->type), dev->id->num,
  1291. part_num, part->name, part->size,
  1292. part->offset);
  1293. if (cur_offs > part->offset)
  1294. part->offset = cur_offs;
  1295. spread_partition(mtd, part, &cur_offs);
  1296. part_num++;
  1297. }
  1298. }
  1299. index_partitions();
  1300. if (generate_mtdparts_save(last_parts, MTDPARTS_MAXLEN) != 0) {
  1301. printf("generated mtdparts too long, resetting to null\n");
  1302. return 1;
  1303. }
  1304. return 0;
  1305. }
  1306. #endif /* CONFIG_CMD_MTDPARTS_SPREAD */
  1307. /**
  1308. * The mtdparts variable tends to be long. If we need to access it
  1309. * before the env is relocated, then we need to use our own stack
  1310. * buffer. gd->env_buf will be too small.
  1311. *
  1312. * @param buf temporary buffer pointer MTDPARTS_MAXLEN long
  1313. * @return mtdparts variable string, NULL if not found
  1314. */
  1315. static const char *env_get_mtdparts(char *buf)
  1316. {
  1317. if (gd->flags & GD_FLG_ENV_READY)
  1318. return env_get("mtdparts");
  1319. if (env_get_f("mtdparts", buf, MTDPARTS_MAXLEN) != -1)
  1320. return buf;
  1321. return NULL;
  1322. }
  1323. /**
  1324. * Accept character string describing mtd partitions and call device_parse()
  1325. * for each entry. Add created devices to the global devices list.
  1326. *
  1327. * @param mtdparts string specifing mtd partitions
  1328. * @return 0 on success, 1 otherwise
  1329. */
  1330. static int parse_mtdparts(const char *const mtdparts)
  1331. {
  1332. const char *p;
  1333. struct mtd_device *dev;
  1334. int err = 1;
  1335. char tmp_parts[MTDPARTS_MAXLEN];
  1336. debug("\n---parse_mtdparts---\nmtdparts = %s\n\n", mtdparts);
  1337. /* delete all devices and partitions */
  1338. if (mtd_devices_init() != 0) {
  1339. printf("could not initialise device list\n");
  1340. return err;
  1341. }
  1342. /* re-read 'mtdparts' variable, mtd_devices_init may be updating env */
  1343. p = env_get_mtdparts(tmp_parts);
  1344. if (!p)
  1345. p = mtdparts;
  1346. if (strncmp(p, "mtdparts=", 9) != 0) {
  1347. printf("mtdparts variable doesn't start with 'mtdparts='\n");
  1348. return err;
  1349. }
  1350. p += 9;
  1351. while (*p != '\0') {
  1352. err = 1;
  1353. if ((device_parse(p, &p, &dev) != 0) || (!dev))
  1354. break;
  1355. debug("+ device: %s\t%d\t%s\n", MTD_DEV_TYPE(dev->id->type),
  1356. dev->id->num, dev->id->mtd_id);
  1357. /* check if parsed device is already on the list */
  1358. if (device_find(dev->id->type, dev->id->num) != NULL) {
  1359. printf("device %s%d redefined, please correct mtdparts variable\n",
  1360. MTD_DEV_TYPE(dev->id->type), dev->id->num);
  1361. break;
  1362. }
  1363. list_add_tail(&dev->link, &devices);
  1364. err = 0;
  1365. }
  1366. if (err == 1) {
  1367. free(dev);
  1368. device_delall(&devices);
  1369. }
  1370. return err;
  1371. }
  1372. /**
  1373. * Parse provided string describing mtdids mapping (see file header for mtdids
  1374. * variable format). Allocate memory for each entry and add all found entries
  1375. * to the global mtdids list.
  1376. *
  1377. * @param ids mapping string
  1378. * @return 0 on success, 1 otherwise
  1379. */
  1380. static int parse_mtdids(const char *const ids)
  1381. {
  1382. const char *p = ids;
  1383. const char *mtd_id;
  1384. int mtd_id_len;
  1385. struct mtdids *id;
  1386. struct list_head *entry, *n;
  1387. struct mtdids *id_tmp;
  1388. u8 type, num;
  1389. u64 size;
  1390. int ret = 1;
  1391. debug("\n---parse_mtdids---\nmtdids = %s\n\n", ids);
  1392. /* clean global mtdids list */
  1393. list_for_each_safe(entry, n, &mtdids) {
  1394. id_tmp = list_entry(entry, struct mtdids, link);
  1395. debug("mtdids del: %d %d\n", id_tmp->type, id_tmp->num);
  1396. list_del(entry);
  1397. free(id_tmp);
  1398. }
  1399. last_ids[0] = '\0';
  1400. INIT_LIST_HEAD(&mtdids);
  1401. while(p && (*p != '\0')) {
  1402. ret = 1;
  1403. /* parse 'nor'|'nand'|'onenand'<dev-num> */
  1404. if (mtd_id_parse(p, &p, &type, &num) != 0)
  1405. break;
  1406. if (*p != '=') {
  1407. printf("mtdids: incorrect <dev-num>\n");
  1408. break;
  1409. }
  1410. p++;
  1411. /* check if requested device exists */
  1412. if (mtd_device_validate(type, num, &size) != 0)
  1413. return 1;
  1414. /* locate <mtd-id> */
  1415. mtd_id = p;
  1416. if ((p = strchr(mtd_id, ',')) != NULL) {
  1417. mtd_id_len = p - mtd_id + 1;
  1418. p++;
  1419. } else {
  1420. mtd_id_len = strlen(mtd_id) + 1;
  1421. }
  1422. if (mtd_id_len == 0) {
  1423. printf("mtdids: no <mtd-id> identifier\n");
  1424. break;
  1425. }
  1426. /* check if this id is already on the list */
  1427. int double_entry = 0;
  1428. list_for_each(entry, &mtdids) {
  1429. id_tmp = list_entry(entry, struct mtdids, link);
  1430. if ((id_tmp->type == type) && (id_tmp->num == num)) {
  1431. double_entry = 1;
  1432. break;
  1433. }
  1434. }
  1435. if (double_entry) {
  1436. printf("device id %s%d redefined, please correct mtdids variable\n",
  1437. MTD_DEV_TYPE(type), num);
  1438. break;
  1439. }
  1440. /* allocate mtdids structure */
  1441. if (!(id = (struct mtdids *)malloc(sizeof(struct mtdids) + mtd_id_len))) {
  1442. printf("out of memory\n");
  1443. break;
  1444. }
  1445. memset(id, 0, sizeof(struct mtdids) + mtd_id_len);
  1446. id->num = num;
  1447. id->type = type;
  1448. id->size = size;
  1449. id->mtd_id = (char *)(id + 1);
  1450. strncpy(id->mtd_id, mtd_id, mtd_id_len - 1);
  1451. id->mtd_id[mtd_id_len - 1] = '\0';
  1452. INIT_LIST_HEAD(&id->link);
  1453. debug("+ id %s%d\t%16lld bytes\t%s\n",
  1454. MTD_DEV_TYPE(id->type), id->num,
  1455. id->size, id->mtd_id);
  1456. list_add_tail(&id->link, &mtdids);
  1457. ret = 0;
  1458. }
  1459. if (ret == 1) {
  1460. /* clean mtdids list and free allocated memory */
  1461. list_for_each_safe(entry, n, &mtdids) {
  1462. id_tmp = list_entry(entry, struct mtdids, link);
  1463. list_del(entry);
  1464. free(id_tmp);
  1465. }
  1466. return 1;
  1467. }
  1468. return 0;
  1469. }
  1470. /**
  1471. * Parse and initialize global mtdids mapping and create global
  1472. * device/partition list.
  1473. *
  1474. * @return 0 on success, 1 otherwise
  1475. */
  1476. int mtdparts_init(void)
  1477. {
  1478. static int initialized = 0;
  1479. const char *ids, *parts;
  1480. const char *current_partition;
  1481. int ids_changed;
  1482. char tmp_ep[PARTITION_MAXLEN + 1];
  1483. char tmp_parts[MTDPARTS_MAXLEN];
  1484. debug("\n---mtdparts_init---\n");
  1485. if (!initialized) {
  1486. INIT_LIST_HEAD(&mtdids);
  1487. INIT_LIST_HEAD(&devices);
  1488. memset(last_ids, 0, sizeof(last_ids));
  1489. memset(last_parts, 0, sizeof(last_parts));
  1490. memset(last_partition, 0, sizeof(last_partition));
  1491. #if defined(CONFIG_SYS_MTDPARTS_RUNTIME)
  1492. board_mtdparts_default(&mtdids_default, &mtdparts_default);
  1493. #endif
  1494. use_defaults = 1;
  1495. initialized = 1;
  1496. }
  1497. /* get variables */
  1498. ids = env_get("mtdids");
  1499. parts = env_get_mtdparts(tmp_parts);
  1500. current_partition = env_get("partition");
  1501. /* save it for later parsing, cannot rely on current partition pointer
  1502. * as 'partition' variable may be updated during init */
  1503. memset(tmp_parts, 0, sizeof(tmp_parts));
  1504. memset(tmp_ep, 0, sizeof(tmp_ep));
  1505. if (current_partition)
  1506. strncpy(tmp_ep, current_partition, PARTITION_MAXLEN);
  1507. debug("last_ids : %s\n", last_ids);
  1508. debug("env_ids : %s\n", ids);
  1509. debug("last_parts: %s\n", last_parts);
  1510. debug("env_parts : %s\n\n", parts);
  1511. debug("last_partition : %s\n", last_partition);
  1512. debug("env_partition : %s\n", current_partition);
  1513. /* if mtdids variable is empty try to use defaults */
  1514. if (!ids) {
  1515. if (mtdids_default) {
  1516. debug("mtdids variable not defined, using default\n");
  1517. ids = mtdids_default;
  1518. env_set("mtdids", (char *)ids);
  1519. } else {
  1520. printf("mtdids not defined, no default present\n");
  1521. return 1;
  1522. }
  1523. }
  1524. if (strlen(ids) > MTDIDS_MAXLEN - 1) {
  1525. printf("mtdids too long (> %d)\n", MTDIDS_MAXLEN);
  1526. return 1;
  1527. }
  1528. /* use defaults when mtdparts variable is not defined
  1529. * once mtdparts is saved environment, drop use_defaults flag */
  1530. if (!parts) {
  1531. if (mtdparts_default && use_defaults) {
  1532. parts = mtdparts_default;
  1533. if (env_set("mtdparts", (char *)parts) == 0)
  1534. use_defaults = 0;
  1535. } else
  1536. printf("mtdparts variable not set, see 'help mtdparts'\n");
  1537. }
  1538. if (parts && (strlen(parts) > MTDPARTS_MAXLEN - 1)) {
  1539. printf("mtdparts too long (> %d)\n", MTDPARTS_MAXLEN);
  1540. return 1;
  1541. }
  1542. /* check if we have already parsed those mtdids */
  1543. if ((last_ids[0] != '\0') && (strcmp(last_ids, ids) == 0)) {
  1544. ids_changed = 0;
  1545. } else {
  1546. ids_changed = 1;
  1547. if (parse_mtdids(ids) != 0) {
  1548. mtd_devices_init();
  1549. return 1;
  1550. }
  1551. /* ok it's good, save new ids */
  1552. strncpy(last_ids, ids, MTDIDS_MAXLEN);
  1553. }
  1554. /* parse partitions if either mtdparts or mtdids were updated */
  1555. if (parts && ((last_parts[0] == '\0') || ((strcmp(last_parts, parts) != 0)) || ids_changed)) {
  1556. if (parse_mtdparts(parts) != 0)
  1557. return 1;
  1558. if (list_empty(&devices)) {
  1559. printf("mtdparts_init: no valid partitions\n");
  1560. return 1;
  1561. }
  1562. /* ok it's good, save new parts */
  1563. strncpy(last_parts, parts, MTDPARTS_MAXLEN);
  1564. /* reset first partition from first dev from the list as current */
  1565. current_mtd_dev = list_entry(devices.next, struct mtd_device, link);
  1566. current_mtd_partnum = 0;
  1567. current_save();
  1568. debug("mtdparts_init: current_mtd_dev = %s%d, current_mtd_partnum = %d\n",
  1569. MTD_DEV_TYPE(current_mtd_dev->id->type),
  1570. current_mtd_dev->id->num, current_mtd_partnum);
  1571. }
  1572. /* mtdparts variable was reset to NULL, delete all devices/partitions */
  1573. if (!parts && (last_parts[0] != '\0'))
  1574. return mtd_devices_init();
  1575. /* do not process current partition if mtdparts variable is null */
  1576. if (!parts)
  1577. return 0;
  1578. /* is current partition set in environment? if so, use it */
  1579. if ((tmp_ep[0] != '\0') && (strcmp(tmp_ep, last_partition) != 0)) {
  1580. struct part_info *p;
  1581. struct mtd_device *cdev;
  1582. u8 pnum;
  1583. debug("--- getting current partition: %s\n", tmp_ep);
  1584. if (find_dev_and_part(tmp_ep, &cdev, &pnum, &p) == 0) {
  1585. current_mtd_dev = cdev;
  1586. current_mtd_partnum = pnum;
  1587. current_save();
  1588. }
  1589. } else if (env_get("partition") == NULL) {
  1590. debug("no partition variable set, setting...\n");
  1591. current_save();
  1592. }
  1593. return 0;
  1594. }
  1595. /**
  1596. * Return pointer to the partition of a requested number from a requested
  1597. * device.
  1598. *
  1599. * @param dev device that is to be searched for a partition
  1600. * @param part_num requested partition number
  1601. * @return pointer to the part_info, NULL otherwise
  1602. */
  1603. static struct part_info* mtd_part_info(struct mtd_device *dev, unsigned int part_num)
  1604. {
  1605. struct list_head *entry;
  1606. struct part_info *part;
  1607. int num;
  1608. if (!dev)
  1609. return NULL;
  1610. debug("\n--- mtd_part_info: partition number %d for device %s%d (%s)\n",
  1611. part_num, MTD_DEV_TYPE(dev->id->type),
  1612. dev->id->num, dev->id->mtd_id);
  1613. if (part_num >= dev->num_parts) {
  1614. printf("invalid partition number %d for device %s%d (%s)\n",
  1615. part_num, MTD_DEV_TYPE(dev->id->type),
  1616. dev->id->num, dev->id->mtd_id);
  1617. return NULL;
  1618. }
  1619. /* locate partition number, return it */
  1620. num = 0;
  1621. list_for_each(entry, &dev->parts) {
  1622. part = list_entry(entry, struct part_info, link);
  1623. if (part_num == num++) {
  1624. return part;
  1625. }
  1626. }
  1627. return NULL;
  1628. }
  1629. /***************************************************/
  1630. /* U-Boot commands */
  1631. /***************************************************/
  1632. /* command line only */
  1633. /**
  1634. * Routine implementing u-boot chpart command. Sets new current partition based
  1635. * on the user supplied partition id. For partition id format see find_dev_and_part().
  1636. *
  1637. * @param cmdtp command internal data
  1638. * @param flag command flag
  1639. * @param argc number of arguments supplied to the command
  1640. * @param argv arguments list
  1641. * @return 0 on success, 1 otherwise
  1642. */
  1643. static int do_chpart(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  1644. {
  1645. /* command line only */
  1646. struct mtd_device *dev;
  1647. struct part_info *part;
  1648. u8 pnum;
  1649. if (mtdparts_init() !=0)
  1650. return 1;
  1651. if (argc < 2) {
  1652. printf("no partition id specified\n");
  1653. return 1;
  1654. }
  1655. if (find_dev_and_part(argv[1], &dev, &pnum, &part) != 0)
  1656. return 1;
  1657. current_mtd_dev = dev;
  1658. current_mtd_partnum = pnum;
  1659. current_save();
  1660. printf("partition changed to %s%d,%d\n",
  1661. MTD_DEV_TYPE(dev->id->type), dev->id->num, pnum);
  1662. return 0;
  1663. }
  1664. /**
  1665. * Routine implementing u-boot mtdparts command. Initialize/update default global
  1666. * partition list and process user partition request (list, add, del).
  1667. *
  1668. * @param cmdtp command internal data
  1669. * @param flag command flag
  1670. * @param argc number of arguments supplied to the command
  1671. * @param argv arguments list
  1672. * @return 0 on success, 1 otherwise
  1673. */
  1674. static int do_mtdparts(cmd_tbl_t *cmdtp, int flag, int argc,
  1675. char * const argv[])
  1676. {
  1677. if (argc == 2) {
  1678. if (strcmp(argv[1], "default") == 0) {
  1679. env_set("mtdids", NULL);
  1680. env_set("mtdparts", NULL);
  1681. env_set("partition", NULL);
  1682. use_defaults = 1;
  1683. mtdparts_init();
  1684. return 0;
  1685. } else if (strcmp(argv[1], "delall") == 0) {
  1686. /* this may be the first run, initialize lists if needed */
  1687. mtdparts_init();
  1688. env_set("mtdparts", NULL);
  1689. /* mtd_devices_init() calls current_save() */
  1690. return mtd_devices_init();
  1691. }
  1692. }
  1693. /* make sure we are in sync with env variables */
  1694. if (mtdparts_init() != 0)
  1695. return 1;
  1696. if (argc == 1) {
  1697. list_partitions();
  1698. return 0;
  1699. }
  1700. /* mtdparts add <mtd-dev> <size>[@<offset>] <name> [ro] */
  1701. if (((argc == 5) || (argc == 6)) && (strncmp(argv[1], "add", 3) == 0)) {
  1702. #define PART_ADD_DESC_MAXLEN 64
  1703. char tmpbuf[PART_ADD_DESC_MAXLEN];
  1704. #if defined(CONFIG_CMD_MTDPARTS_SPREAD)
  1705. struct mtd_info *mtd;
  1706. uint64_t next_offset;
  1707. #endif
  1708. u8 type, num, len;
  1709. struct mtd_device *dev;
  1710. struct mtd_device *dev_tmp;
  1711. struct mtdids *id;
  1712. struct part_info *p;
  1713. if (mtd_id_parse(argv[2], NULL, &type, &num) != 0)
  1714. return 1;
  1715. if ((id = id_find(type, num)) == NULL) {
  1716. printf("no such device %s defined in mtdids variable\n", argv[2]);
  1717. return 1;
  1718. }
  1719. len = strlen(id->mtd_id) + 1; /* 'mtd_id:' */
  1720. len += strlen(argv[3]); /* size@offset */
  1721. len += strlen(argv[4]) + 2; /* '(' name ')' */
  1722. if (argv[5] && (strlen(argv[5]) == 2))
  1723. len += 2; /* 'ro' */
  1724. if (len >= PART_ADD_DESC_MAXLEN) {
  1725. printf("too long partition description\n");
  1726. return 1;
  1727. }
  1728. sprintf(tmpbuf, "%s:%s(%s)%s",
  1729. id->mtd_id, argv[3], argv[4], argv[5] ? argv[5] : "");
  1730. debug("add tmpbuf: %s\n", tmpbuf);
  1731. if ((device_parse(tmpbuf, NULL, &dev) != 0) || (!dev))
  1732. return 1;
  1733. debug("+ %s\t%d\t%s\n", MTD_DEV_TYPE(dev->id->type),
  1734. dev->id->num, dev->id->mtd_id);
  1735. p = list_entry(dev->parts.next, struct part_info, link);
  1736. #if defined(CONFIG_CMD_MTDPARTS_SPREAD)
  1737. if (get_mtd_info(dev->id->type, dev->id->num, &mtd))
  1738. return 1;
  1739. if (!strcmp(&argv[1][3], ".spread")) {
  1740. spread_partition(mtd, p, &next_offset);
  1741. debug("increased %s to %llu bytes\n", p->name, p->size);
  1742. }
  1743. #endif
  1744. dev_tmp = device_find(dev->id->type, dev->id->num);
  1745. if (dev_tmp == NULL) {
  1746. device_add(dev);
  1747. } else if (part_add(dev_tmp, p) != 0) {
  1748. /* merge new partition with existing ones*/
  1749. device_del(dev);
  1750. return 1;
  1751. }
  1752. if (generate_mtdparts_save(last_parts, MTDPARTS_MAXLEN) != 0) {
  1753. printf("generated mtdparts too long, resetting to null\n");
  1754. return 1;
  1755. }
  1756. return 0;
  1757. }
  1758. /* mtdparts del part-id */
  1759. if ((argc == 3) && (strcmp(argv[1], "del") == 0)) {
  1760. debug("del: part-id = %s\n", argv[2]);
  1761. return delete_partition(argv[2]);
  1762. }
  1763. #if defined(CONFIG_CMD_MTDPARTS_SPREAD)
  1764. if ((argc == 2) && (strcmp(argv[1], "spread") == 0))
  1765. return spread_partitions();
  1766. #endif /* CONFIG_CMD_MTDPARTS_SPREAD */
  1767. return CMD_RET_USAGE;
  1768. }
  1769. /***************************************************/
  1770. U_BOOT_CMD(
  1771. chpart, 2, 0, do_chpart,
  1772. "change active partition",
  1773. "part-id\n"
  1774. " - change active partition (e.g. part-id = nand0,1)"
  1775. );
  1776. #ifdef CONFIG_SYS_LONGHELP
  1777. static char mtdparts_help_text[] =
  1778. "\n"
  1779. " - list partition table\n"
  1780. "mtdparts delall\n"
  1781. " - delete all partitions\n"
  1782. "mtdparts del part-id\n"
  1783. " - delete partition (e.g. part-id = nand0,1)\n"
  1784. "mtdparts add <mtd-dev> <size>[@<offset>] [<name>] [ro]\n"
  1785. " - add partition\n"
  1786. #if defined(CONFIG_CMD_MTDPARTS_SPREAD)
  1787. "mtdparts add.spread <mtd-dev> <size>[@<offset>] [<name>] [ro]\n"
  1788. " - add partition, padding size by skipping bad blocks\n"
  1789. #endif
  1790. "mtdparts default\n"
  1791. " - reset partition table to defaults\n"
  1792. #if defined(CONFIG_CMD_MTDPARTS_SPREAD)
  1793. "mtdparts spread\n"
  1794. " - adjust the sizes of the partitions so they are\n"
  1795. " at least as big as the mtdparts variable specifies\n"
  1796. " and they each start on a good block\n\n"
  1797. #else
  1798. "\n"
  1799. #endif /* CONFIG_CMD_MTDPARTS_SPREAD */
  1800. "-----\n\n"
  1801. "this command uses three environment variables:\n\n"
  1802. "'partition' - keeps current partition identifier\n\n"
  1803. "partition := <part-id>\n"
  1804. "<part-id> := <dev-id>,part_num\n\n"
  1805. "'mtdids' - linux kernel mtd device id <-> u-boot device id mapping\n\n"
  1806. "mtdids=<idmap>[,<idmap>,...]\n\n"
  1807. "<idmap> := <dev-id>=<mtd-id>\n"
  1808. "<dev-id> := 'nand'|'nor'|'onenand'<dev-num>\n"
  1809. "<dev-num> := mtd device number, 0...\n"
  1810. "<mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)\n\n"
  1811. "'mtdparts' - partition list\n\n"
  1812. "mtdparts=mtdparts=<mtd-def>[;<mtd-def>...]\n\n"
  1813. "<mtd-def> := <mtd-id>:<part-def>[,<part-def>...]\n"
  1814. "<mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)\n"
  1815. "<part-def> := <size>[@<offset>][<name>][<ro-flag>]\n"
  1816. "<size> := standard linux memsize OR '-' to denote all remaining space\n"
  1817. "<offset> := partition start offset within the device\n"
  1818. "<name> := '(' NAME ')'\n"
  1819. "<ro-flag> := when set to 'ro' makes partition read-only (not used, passed to kernel)";
  1820. #endif
  1821. U_BOOT_CMD(
  1822. mtdparts, 6, 0, do_mtdparts,
  1823. "define flash/nand partitions", mtdparts_help_text
  1824. );
  1825. /***************************************************/