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