mtdparts.c 52 KB

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