cmd_jffs2.c 54 KB

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