mtdparts.c 53 KB

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