cmd_jffs2.c 53 KB

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