cmd_jffs2.c 54 KB

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