cmd_jffs2.c 56 KB

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