cmd_mtdparts.c 52 KB

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