cmd_jffs2.c 55 KB

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