cmd_mtdparts.c 47 KB

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