cmd_jffs2.c 54 KB

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