mtdparts.c 53 KB

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