cmd_jffs2.c 58 KB

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