cmd_jffs2.c 54 KB

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