cmd_jffs2.c 52 KB

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