cmd_mtdparts.c 52 KB

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