cmd_mtdparts.c 52 KB

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