mtdparts.c 53 KB

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