cmd_mtdparts.c 47 KB

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