mtdparts.c 53 KB

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