cmd_mtdparts.c 52 KB

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