cmd_jffs2.c 56 KB

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