cmd_mtdparts.c 47 KB

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