12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099 |
- /*
- * (C) Copyright 2002
- * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
- *
- * (C) Copyright 2002
- * Robert Schwebel, Pengutronix, <r.schwebel@pengutronix.de>
- *
- * (C) Copyright 2003
- * Kai-Uwe Bloem, Auerswald GmbH & Co KG, <linux-development@auerswald.de>
- *
- * (C) Copyright 2005
- * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
- *
- * Added support for reading flash partition table from environment.
- * Parsing routines are based on driver/mtd/cmdline.c from the linux 2.4
- * kernel tree.
- *
- * (C) Copyright 2008
- * Harald Welte, OpenMoko, Inc., Harald Welte <laforge@openmoko.org>
- *
- * $Id: cmdlinepart.c,v 1.17 2004/11/26 11:18:47 lavinen Exp $
- * Copyright 2002 SYSGO Real-Time Solutions GmbH
- *
- * See file CREDITS for list of people who contributed to this
- * project.
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of
- * the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
- * MA 02111-1307 USA
- */
- /*
- * Three environment variables are used by the parsing routines:
- *
- * 'partition' - keeps current partition identifier
- *
- * partition := <part-id>
- * <part-id> := <dev-id>,part_num
- *
- *
- * 'mtdids' - linux kernel mtd device id <-> u-boot device id mapping
- *
- * mtdids=<idmap>[,<idmap>,...]
- *
- * <idmap> := <dev-id>=<mtd-id>
- * <dev-id> := 'nand'|'nor'|'onenand'<dev-num>
- * <dev-num> := mtd device number, 0...
- * <mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)
- *
- *
- * 'mtdparts' - partition list
- *
- * mtdparts=mtdparts=<mtd-def>[;<mtd-def>...]
- *
- * <mtd-def> := <mtd-id>:<part-def>[,<part-def>...]
- * <mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)
- * <part-def> := <size>[@<offset>][<name>][<ro-flag>]
- * <size> := standard linux memsize OR '-' to denote all remaining space
- * <offset> := partition start offset within the device
- * <name> := '(' NAME ')'
- * <ro-flag> := when set to 'ro' makes partition read-only (not used, passed to kernel)
- *
- * Notes:
- * - each <mtd-id> used in mtdparts must albo exist in 'mtddis' mapping
- * - if the above variables are not set defaults for a given target are used
- *
- * Examples:
- *
- * 1 NOR Flash, with 1 single writable partition:
- * mtdids=nor0=edb7312-nor
- * mtdparts=mtdparts=edb7312-nor:-
- *
- * 1 NOR Flash with 2 partitions, 1 NAND with one
- * mtdids=nor0=edb7312-nor,nand0=edb7312-nand
- * mtdparts=mtdparts=edb7312-nor:256k(ARMboot)ro,-(root);edb7312-nand:-(home)
- *
- */
- #include <common.h>
- #include <command.h>
- #include <malloc.h>
- #include <jffs2/load_kernel.h>
- #include <linux/list.h>
- #include <linux/ctype.h>
- #include <linux/err.h>
- #include <linux/mtd/mtd.h>
- #if defined(CONFIG_CMD_NAND)
- #include <linux/mtd/nand.h>
- #include <nand.h>
- #endif
- #if defined(CONFIG_CMD_ONENAND)
- #include <linux/mtd/onenand.h>
- #include <onenand_uboot.h>
- #endif
- /* special size referring to all the remaining space in a partition */
- #define SIZE_REMAINING 0xFFFFFFFF
- /* special offset value, it is used when not provided by user
- *
- * this value is used temporarily during parsing, later such offests
- * are recalculated */
- #define OFFSET_NOT_SPECIFIED 0xFFFFFFFF
- /* minimum partition size */
- #define MIN_PART_SIZE 4096
- /* this flag needs to be set in part_info struct mask_flags
- * field for read-only partitions */
- #define MTD_WRITEABLE_CMD 1
- /* default values for mtdids and mtdparts variables */
- #if defined(MTDIDS_DEFAULT)
- static const char *const mtdids_default = MTDIDS_DEFAULT;
- #else
- static const char *const mtdids_default = NULL;
- #endif
- #if defined(MTDPARTS_DEFAULT)
- static const char *const mtdparts_default = MTDPARTS_DEFAULT;
- #else
- static const char *const mtdparts_default = NULL;
- #endif
- /* copies of last seen 'mtdids', 'mtdparts' and 'partition' env variables */
- #define MTDIDS_MAXLEN 128
- #define MTDPARTS_MAXLEN 512
- #define PARTITION_MAXLEN 16
- static char last_ids[MTDIDS_MAXLEN];
- static char last_parts[MTDPARTS_MAXLEN];
- static char last_partition[PARTITION_MAXLEN];
- /* low level jffs2 cache cleaning routine */
- extern void jffs2_free_cache(struct part_info *part);
- /* mtdids mapping list, filled by parse_ids() */
- static struct list_head mtdids;
- /* device/partition list, parse_cmdline() parses into here */
- static struct list_head devices;
- /* current active device and partition number */
- struct mtd_device *current_mtd_dev = NULL;
- u8 current_mtd_partnum = 0;
- static struct part_info* mtd_part_info(struct mtd_device *dev, unsigned int part_num);
- /* command line only routines */
- static struct mtdids* id_find_by_mtd_id(const char *mtd_id, unsigned int mtd_id_len);
- static int device_del(struct mtd_device *dev);
- /**
- * Parses a string into a number. The number stored at ptr is
- * potentially suffixed with K (for kilobytes, or 1024 bytes),
- * M (for megabytes, or 1048576 bytes), or G (for gigabytes, or
- * 1073741824). If the number is suffixed with K, M, or G, then
- * the return value is the number multiplied by one kilobyte, one
- * megabyte, or one gigabyte, respectively.
- *
- * @param ptr where parse begins
- * @param retptr output pointer to next char after parse completes (output)
- * @return resulting unsigned int
- */
- static unsigned long memsize_parse (const char *const ptr, const char **retptr)
- {
- unsigned long ret = simple_strtoul(ptr, (char **)retptr, 0);
- switch (**retptr) {
- case 'G':
- case 'g':
- ret <<= 10;
- case 'M':
- case 'm':
- ret <<= 10;
- case 'K':
- case 'k':
- ret <<= 10;
- (*retptr)++;
- default:
- break;
- }
- return ret;
- }
- /**
- * Format string describing supplied size. This routine does the opposite job
- * to memsize_parse(). Size in bytes is converted to string and if possible
- * shortened by using k (kilobytes), m (megabytes) or g (gigabytes) suffix.
- *
- * Note, that this routine does not check for buffer overflow, it's the caller
- * who must assure enough space.
- *
- * @param buf output buffer
- * @param size size to be converted to string
- */
- static void memsize_format(char *buf, u32 size)
- {
- #define SIZE_GB ((u32)1024*1024*1024)
- #define SIZE_MB ((u32)1024*1024)
- #define SIZE_KB ((u32)1024)
- if ((size % SIZE_GB) == 0)
- sprintf(buf, "%ug", size/SIZE_GB);
- else if ((size % SIZE_MB) == 0)
- sprintf(buf, "%um", size/SIZE_MB);
- else if (size % SIZE_KB == 0)
- sprintf(buf, "%uk", size/SIZE_KB);
- else
- sprintf(buf, "%u", size);
- }
- /**
- * This routine does global indexing of all partitions. Resulting index for
- * current partition is saved in 'mtddevnum'. Current partition name in
- * 'mtddevname'.
- */
- static void index_partitions(void)
- {
- u16 mtddevnum;
- struct part_info *part;
- struct list_head *dentry;
- struct mtd_device *dev;
- debug("--- index partitions ---\n");
- if (current_mtd_dev) {
- mtddevnum = 0;
- list_for_each(dentry, &devices) {
- dev = list_entry(dentry, struct mtd_device, link);
- if (dev == current_mtd_dev) {
- mtddevnum += current_mtd_partnum;
- setenv_ulong("mtddevnum", mtddevnum);
- break;
- }
- mtddevnum += dev->num_parts;
- }
- part = mtd_part_info(current_mtd_dev, current_mtd_partnum);
- setenv("mtddevname", part->name);
- debug("=> mtddevnum %d,\n=> mtddevname %s\n", mtddevnum, part->name);
- } else {
- setenv("mtddevnum", NULL);
- setenv("mtddevname", NULL);
- debug("=> mtddevnum NULL\n=> mtddevname NULL\n");
- }
- }
- /**
- * Save current device and partition in environment variable 'partition'.
- */
- static void current_save(void)
- {
- char buf[16];
- debug("--- current_save ---\n");
- if (current_mtd_dev) {
- sprintf(buf, "%s%d,%d", MTD_DEV_TYPE(current_mtd_dev->id->type),
- current_mtd_dev->id->num, current_mtd_partnum);
- setenv("partition", buf);
- strncpy(last_partition, buf, 16);
- debug("=> partition %s\n", buf);
- } else {
- setenv("partition", NULL);
- last_partition[0] = '\0';
- debug("=> partition NULL\n");
- }
- index_partitions();
- }
- /**
- * Produce a mtd_info given a type and num.
- *
- * @param type mtd type
- * @param num mtd number
- * @param mtd a pointer to an mtd_info instance (output)
- * @return 0 if device is valid, 1 otherwise
- */
- static int get_mtd_info(u8 type, u8 num, struct mtd_info **mtd)
- {
- char mtd_dev[16];
- sprintf(mtd_dev, "%s%d", MTD_DEV_TYPE(type), num);
- *mtd = get_mtd_device_nm(mtd_dev);
- if (IS_ERR(*mtd)) {
- printf("Device %s not found!\n", mtd_dev);
- return 1;
- }
- return 0;
- }
- /**
- * Performs sanity check for supplied flash partition.
- * Table of existing MTD flash devices is searched and partition device
- * is located. Alignment with the granularity of nand erasesize is verified.
- *
- * @param id of the parent device
- * @param part partition to validate
- * @return 0 if partition is valid, 1 otherwise
- */
- static int part_validate_eraseblock(struct mtdids *id, struct part_info *part)
- {
- struct mtd_info *mtd = NULL;
- int i, j;
- ulong start;
- if (get_mtd_info(id->type, id->num, &mtd))
- return 1;
- part->sector_size = mtd->erasesize;
- if (!mtd->numeraseregions) {
- /*
- * Only one eraseregion (NAND, OneNAND or uniform NOR),
- * checking for alignment is easy here
- */
- if ((unsigned long)part->offset % mtd->erasesize) {
- printf("%s%d: partition (%s) start offset"
- "alignment incorrect\n",
- MTD_DEV_TYPE(id->type), id->num, part->name);
- return 1;
- }
- if (part->size % mtd->erasesize) {
- printf("%s%d: partition (%s) size alignment incorrect\n",
- MTD_DEV_TYPE(id->type), id->num, part->name);
- return 1;
- }
- } else {
- /*
- * Multiple eraseregions (non-uniform NOR),
- * checking for alignment is more complex here
- */
- /* Check start alignment */
- for (i = 0; i < mtd->numeraseregions; i++) {
- start = mtd->eraseregions[i].offset;
- for (j = 0; j < mtd->eraseregions[i].numblocks; j++) {
- if (part->offset == start)
- goto start_ok;
- start += mtd->eraseregions[i].erasesize;
- }
- }
- printf("%s%d: partition (%s) start offset alignment incorrect\n",
- MTD_DEV_TYPE(id->type), id->num, part->name);
- return 1;
- start_ok:
- /* Check end/size alignment */
- for (i = 0; i < mtd->numeraseregions; i++) {
- start = mtd->eraseregions[i].offset;
- for (j = 0; j < mtd->eraseregions[i].numblocks; j++) {
- if ((part->offset + part->size) == start)
- goto end_ok;
- start += mtd->eraseregions[i].erasesize;
- }
- }
- /* Check last sector alignment */
- if ((part->offset + part->size) == start)
- goto end_ok;
- printf("%s%d: partition (%s) size alignment incorrect\n",
- MTD_DEV_TYPE(id->type), id->num, part->name);
- return 1;
- end_ok:
- return 0;
- }
- return 0;
- }
- /**
- * Performs sanity check for supplied partition. Offset and size are verified
- * to be within valid range. Partition type is checked and either
- * parts_validate_nor() or parts_validate_nand() is called with the argument
- * of part.
- *
- * @param id of the parent device
- * @param part partition to validate
- * @return 0 if partition is valid, 1 otherwise
- */
- static int part_validate(struct mtdids *id, struct part_info *part)
- {
- if (part->size == SIZE_REMAINING)
- part->size = id->size - part->offset;
- if (part->offset > id->size) {
- printf("%s: offset %08x beyond flash size %08x\n",
- id->mtd_id, part->offset, id->size);
- return 1;
- }
- if ((part->offset + part->size) <= part->offset) {
- printf("%s%d: partition (%s) size too big\n",
- MTD_DEV_TYPE(id->type), id->num, part->name);
- return 1;
- }
- if (part->offset + part->size > id->size) {
- printf("%s: partitioning exceeds flash size\n", id->mtd_id);
- return 1;
- }
- /*
- * Now we need to check if the partition starts and ends on
- * sector (eraseblock) regions
- */
- return part_validate_eraseblock(id, part);
- }
- /**
- * Delete selected partition from the partion list of the specified device.
- *
- * @param dev device to delete partition from
- * @param part partition to delete
- * @return 0 on success, 1 otherwise
- */
- static int part_del(struct mtd_device *dev, struct part_info *part)
- {
- u8 current_save_needed = 0;
- /* if there is only one partition, remove whole device */
- if (dev->num_parts == 1)
- return device_del(dev);
- /* otherwise just delete this partition */
- if (dev == current_mtd_dev) {
- /* we are modyfing partitions for the current device,
- * update current */
- struct part_info *curr_pi;
- curr_pi = mtd_part_info(current_mtd_dev, current_mtd_partnum);
- if (curr_pi) {
- if (curr_pi == part) {
- printf("current partition deleted, resetting current to 0\n");
- current_mtd_partnum = 0;
- } else if (part->offset <= curr_pi->offset) {
- current_mtd_partnum--;
- }
- current_save_needed = 1;
- }
- }
- list_del(&part->link);
- free(part);
- dev->num_parts--;
- if (current_save_needed > 0)
- current_save();
- else
- index_partitions();
- return 0;
- }
- /**
- * Delete all partitions from parts head list, free memory.
- *
- * @param head list of partitions to delete
- */
- static void part_delall(struct list_head *head)
- {
- struct list_head *entry, *n;
- struct part_info *part_tmp;
- /* clean tmp_list and free allocated memory */
- list_for_each_safe(entry, n, head) {
- part_tmp = list_entry(entry, struct part_info, link);
- list_del(entry);
- free(part_tmp);
- }
- }
- /**
- * Add new partition to the supplied partition list. Make sure partitions are
- * sorted by offset in ascending order.
- *
- * @param head list this partition is to be added to
- * @param new partition to be added
- */
- static int part_sort_add(struct mtd_device *dev, struct part_info *part)
- {
- struct list_head *entry;
- struct part_info *new_pi, *curr_pi;
- /* link partition to parrent dev */
- part->dev = dev;
- if (list_empty(&dev->parts)) {
- debug("part_sort_add: list empty\n");
- list_add(&part->link, &dev->parts);
- dev->num_parts++;
- index_partitions();
- return 0;
- }
- new_pi = list_entry(&part->link, struct part_info, link);
- /* get current partition info if we are updating current device */
- curr_pi = NULL;
- if (dev == current_mtd_dev)
- curr_pi = mtd_part_info(current_mtd_dev, current_mtd_partnum);
- list_for_each(entry, &dev->parts) {
- struct part_info *pi;
- pi = list_entry(entry, struct part_info, link);
- /* be compliant with kernel cmdline, allow only one partition at offset zero */
- if ((new_pi->offset == pi->offset) && (pi->offset == 0)) {
- printf("cannot add second partition at offset 0\n");
- return 1;
- }
- if (new_pi->offset <= pi->offset) {
- list_add_tail(&part->link, entry);
- dev->num_parts++;
- if (curr_pi && (pi->offset <= curr_pi->offset)) {
- /* we are modyfing partitions for the current
- * device, update current */
- current_mtd_partnum++;
- current_save();
- } else {
- index_partitions();
- }
- return 0;
- }
- }
- list_add_tail(&part->link, &dev->parts);
- dev->num_parts++;
- index_partitions();
- return 0;
- }
- /**
- * Add provided partition to the partition list of a given device.
- *
- * @param dev device to which partition is added
- * @param part partition to be added
- * @return 0 on success, 1 otherwise
- */
- static int part_add(struct mtd_device *dev, struct part_info *part)
- {
- /* verify alignment and size */
- if (part_validate(dev->id, part) != 0)
- return 1;
- /* partition is ok, add it to the list */
- if (part_sort_add(dev, part) != 0)
- return 1;
- return 0;
- }
- /**
- * Parse one partition definition, allocate memory and return pointer to this
- * location in retpart.
- *
- * @param partdef pointer to the partition definition string i.e. <part-def>
- * @param ret output pointer to next char after parse completes (output)
- * @param retpart pointer to the allocated partition (output)
- * @return 0 on success, 1 otherwise
- */
- static int part_parse(const char *const partdef, const char **ret, struct part_info **retpart)
- {
- struct part_info *part;
- unsigned long size;
- unsigned long offset;
- const char *name;
- int name_len;
- unsigned int mask_flags;
- const char *p;
- p = partdef;
- *retpart = NULL;
- *ret = NULL;
- /* fetch the partition size */
- if (*p == '-') {
- /* assign all remaining space to this partition */
- debug("'-': remaining size assigned\n");
- size = SIZE_REMAINING;
- p++;
- } else {
- size = memsize_parse(p, &p);
- if (size < MIN_PART_SIZE) {
- printf("partition size too small (%lx)\n", size);
- return 1;
- }
- }
- /* check for offset */
- offset = OFFSET_NOT_SPECIFIED;
- if (*p == '@') {
- p++;
- offset = memsize_parse(p, &p);
- }
- /* now look for the name */
- if (*p == '(') {
- name = ++p;
- if ((p = strchr(name, ')')) == NULL) {
- printf("no closing ) found in partition name\n");
- return 1;
- }
- name_len = p - name + 1;
- if ((name_len - 1) == 0) {
- printf("empty partition name\n");
- return 1;
- }
- p++;
- } else {
- /* 0x00000000@0x00000000 */
- name_len = 22;
- name = NULL;
- }
- /* test for options */
- mask_flags = 0;
- if (strncmp(p, "ro", 2) == 0) {
- mask_flags |= MTD_WRITEABLE_CMD;
- p += 2;
- }
- /* check for next partition definition */
- if (*p == ',') {
- if (size == SIZE_REMAINING) {
- *ret = NULL;
- printf("no partitions allowed after a fill-up partition\n");
- return 1;
- }
- *ret = ++p;
- } else if ((*p == ';') || (*p == '\0')) {
- *ret = p;
- } else {
- printf("unexpected character '%c' at the end of partition\n", *p);
- *ret = NULL;
- return 1;
- }
- /* allocate memory */
- part = (struct part_info *)malloc(sizeof(struct part_info) + name_len);
- if (!part) {
- printf("out of memory\n");
- return 1;
- }
- memset(part, 0, sizeof(struct part_info) + name_len);
- part->size = size;
- part->offset = offset;
- part->mask_flags = mask_flags;
- part->name = (char *)(part + 1);
- if (name) {
- /* copy user provided name */
- strncpy(part->name, name, name_len - 1);
- part->auto_name = 0;
- } else {
- /* auto generated name in form of size@offset */
- sprintf(part->name, "0x%08lx@0x%08lx", size, offset);
- part->auto_name = 1;
- }
- part->name[name_len - 1] = '\0';
- INIT_LIST_HEAD(&part->link);
- debug("+ partition: name %-22s size 0x%08x offset 0x%08x mask flags %d\n",
- part->name, part->size,
- part->offset, part->mask_flags);
- *retpart = part;
- return 0;
- }
- /**
- * Check device number to be within valid range for given device type.
- *
- * @param type mtd type
- * @param num mtd number
- * @param size a pointer to the size of the mtd device (output)
- * @return 0 if device is valid, 1 otherwise
- */
- static int mtd_device_validate(u8 type, u8 num, u32 *size)
- {
- struct mtd_info *mtd = NULL;
- if (get_mtd_info(type, num, &mtd))
- return 1;
- *size = mtd->size;
- return 0;
- }
- /**
- * Delete all mtd devices from a supplied devices list, free memory allocated for
- * each device and delete all device partitions.
- *
- * @return 0 on success, 1 otherwise
- */
- static int device_delall(struct list_head *head)
- {
- struct list_head *entry, *n;
- struct mtd_device *dev_tmp;
- /* clean devices list */
- list_for_each_safe(entry, n, head) {
- dev_tmp = list_entry(entry, struct mtd_device, link);
- list_del(entry);
- part_delall(&dev_tmp->parts);
- free(dev_tmp);
- }
- INIT_LIST_HEAD(&devices);
- return 0;
- }
- /**
- * If provided device exists it's partitions are deleted, device is removed
- * from device list and device memory is freed.
- *
- * @param dev device to be deleted
- * @return 0 on success, 1 otherwise
- */
- static int device_del(struct mtd_device *dev)
- {
- part_delall(&dev->parts);
- list_del(&dev->link);
- free(dev);
- if (dev == current_mtd_dev) {
- /* we just deleted current device */
- if (list_empty(&devices)) {
- current_mtd_dev = NULL;
- } else {
- /* reset first partition from first dev from the
- * devices list as current */
- current_mtd_dev = list_entry(devices.next, struct mtd_device, link);
- current_mtd_partnum = 0;
- }
- current_save();
- return 0;
- }
- index_partitions();
- return 0;
- }
- /**
- * Search global device list and return pointer to the device of type and num
- * specified.
- *
- * @param type device type
- * @param num device number
- * @return NULL if requested device does not exist
- */
- struct mtd_device *device_find(u8 type, u8 num)
- {
- struct list_head *entry;
- struct mtd_device *dev_tmp;
- list_for_each(entry, &devices) {
- dev_tmp = list_entry(entry, struct mtd_device, link);
- if ((dev_tmp->id->type == type) && (dev_tmp->id->num == num))
- return dev_tmp;
- }
- return NULL;
- }
- /**
- * Add specified device to the global device list.
- *
- * @param dev device to be added
- */
- static void device_add(struct mtd_device *dev)
- {
- u8 current_save_needed = 0;
- if (list_empty(&devices)) {
- current_mtd_dev = dev;
- current_mtd_partnum = 0;
- current_save_needed = 1;
- }
- list_add_tail(&dev->link, &devices);
- if (current_save_needed > 0)
- current_save();
- else
- index_partitions();
- }
- /**
- * Parse device type, name and mtd-id. If syntax is ok allocate memory and
- * return pointer to the device structure.
- *
- * @param mtd_dev pointer to the device definition string i.e. <mtd-dev>
- * @param ret output pointer to next char after parse completes (output)
- * @param retdev pointer to the allocated device (output)
- * @return 0 on success, 1 otherwise
- */
- static int device_parse(const char *const mtd_dev, const char **ret, struct mtd_device **retdev)
- {
- struct mtd_device *dev;
- struct part_info *part;
- struct mtdids *id;
- const char *mtd_id;
- unsigned int mtd_id_len;
- const char *p;
- const char *pend;
- LIST_HEAD(tmp_list);
- struct list_head *entry, *n;
- u16 num_parts;
- u32 offset;
- int err = 1;
- debug("===device_parse===\n");
- assert(retdev);
- *retdev = NULL;
- if (ret)
- *ret = NULL;
- /* fetch <mtd-id> */
- mtd_id = p = mtd_dev;
- if (!(p = strchr(mtd_id, ':'))) {
- printf("no <mtd-id> identifier\n");
- return 1;
- }
- mtd_id_len = p - mtd_id + 1;
- p++;
- /* verify if we have a valid device specified */
- if ((id = id_find_by_mtd_id(mtd_id, mtd_id_len - 1)) == NULL) {
- printf("invalid mtd device '%.*s'\n", mtd_id_len - 1, mtd_id);
- return 1;
- }
- #ifdef DEBUG
- pend = strchr(p, ';');
- #endif
- debug("dev type = %d (%s), dev num = %d, mtd-id = %s\n",
- id->type, MTD_DEV_TYPE(id->type),
- id->num, id->mtd_id);
- debug("parsing partitions %.*s\n", (pend ? pend - p : strlen(p)), p);
- /* parse partitions */
- num_parts = 0;
- offset = 0;
- if ((dev = device_find(id->type, id->num)) != NULL) {
- /* if device already exists start at the end of the last partition */
- part = list_entry(dev->parts.prev, struct part_info, link);
- offset = part->offset + part->size;
- }
- while (p && (*p != '\0') && (*p != ';')) {
- err = 1;
- if ((part_parse(p, &p, &part) != 0) || (!part))
- break;
- /* calculate offset when not specified */
- if (part->offset == OFFSET_NOT_SPECIFIED)
- part->offset = offset;
- else
- offset = part->offset;
- /* verify alignment and size */
- if (part_validate(id, part) != 0)
- break;
- offset += part->size;
- /* partition is ok, add it to the list */
- list_add_tail(&part->link, &tmp_list);
- num_parts++;
- err = 0;
- }
- if (err == 1) {
- part_delall(&tmp_list);
- return 1;
- }
- if (num_parts == 0) {
- printf("no partitions for device %s%d (%s)\n",
- MTD_DEV_TYPE(id->type), id->num, id->mtd_id);
- return 1;
- }
- debug("\ntotal partitions: %d\n", num_parts);
- /* check for next device presence */
- if (p) {
- if (*p == ';') {
- if (ret)
- *ret = ++p;
- } else if (*p == '\0') {
- if (ret)
- *ret = p;
- } else {
- printf("unexpected character '%c' at the end of device\n", *p);
- if (ret)
- *ret = NULL;
- return 1;
- }
- }
- /* allocate memory for mtd_device structure */
- if ((dev = (struct mtd_device *)malloc(sizeof(struct mtd_device))) == NULL) {
- printf("out of memory\n");
- return 1;
- }
- memset(dev, 0, sizeof(struct mtd_device));
- dev->id = id;
- dev->num_parts = 0; /* part_sort_add increments num_parts */
- INIT_LIST_HEAD(&dev->parts);
- INIT_LIST_HEAD(&dev->link);
- /* move partitions from tmp_list to dev->parts */
- list_for_each_safe(entry, n, &tmp_list) {
- part = list_entry(entry, struct part_info, link);
- list_del(entry);
- if (part_sort_add(dev, part) != 0) {
- device_del(dev);
- return 1;
- }
- }
- *retdev = dev;
- debug("===\n\n");
- return 0;
- }
- /**
- * Initialize global device list.
- *
- * @return 0 on success, 1 otherwise
- */
- static int mtd_devices_init(void)
- {
- last_parts[0] = '\0';
- current_mtd_dev = NULL;
- current_save();
- return device_delall(&devices);
- }
- /*
- * Search global mtdids list and find id of requested type and number.
- *
- * @return pointer to the id if it exists, NULL otherwise
- */
- static struct mtdids* id_find(u8 type, u8 num)
- {
- struct list_head *entry;
- struct mtdids *id;
- list_for_each(entry, &mtdids) {
- id = list_entry(entry, struct mtdids, link);
- if ((id->type == type) && (id->num == num))
- return id;
- }
- return NULL;
- }
- /**
- * Search global mtdids list and find id of a requested mtd_id.
- *
- * Note: first argument is not null terminated.
- *
- * @param mtd_id string containing requested mtd_id
- * @param mtd_id_len length of supplied mtd_id
- * @return pointer to the id if it exists, NULL otherwise
- */
- static struct mtdids* id_find_by_mtd_id(const char *mtd_id, unsigned int mtd_id_len)
- {
- struct list_head *entry;
- struct mtdids *id;
- debug("--- id_find_by_mtd_id: '%.*s' (len = %d)\n",
- mtd_id_len, mtd_id, mtd_id_len);
- list_for_each(entry, &mtdids) {
- id = list_entry(entry, struct mtdids, link);
- debug("entry: '%s' (len = %d)\n",
- id->mtd_id, strlen(id->mtd_id));
- if (mtd_id_len != strlen(id->mtd_id))
- continue;
- if (strncmp(id->mtd_id, mtd_id, mtd_id_len) == 0)
- return id;
- }
- return NULL;
- }
- /**
- * Parse device id string <dev-id> := 'nand'|'nor'|'onenand'<dev-num>,
- * return device type and number.
- *
- * @param id string describing device id
- * @param ret_id output pointer to next char after parse completes (output)
- * @param dev_type parsed device type (output)
- * @param dev_num parsed device number (output)
- * @return 0 on success, 1 otherwise
- */
- int mtd_id_parse(const char *id, const char **ret_id, u8 *dev_type,
- u8 *dev_num)
- {
- const char *p = id;
- *dev_type = 0;
- if (strncmp(p, "nand", 4) == 0) {
- *dev_type = MTD_DEV_TYPE_NAND;
- p += 4;
- } else if (strncmp(p, "nor", 3) == 0) {
- *dev_type = MTD_DEV_TYPE_NOR;
- p += 3;
- } else if (strncmp(p, "onenand", 7) == 0) {
- *dev_type = MTD_DEV_TYPE_ONENAND;
- p += 7;
- } else {
- printf("incorrect device type in %s\n", id);
- return 1;
- }
- if (!isdigit(*p)) {
- printf("incorrect device number in %s\n", id);
- return 1;
- }
- *dev_num = simple_strtoul(p, (char **)&p, 0);
- if (ret_id)
- *ret_id = p;
- return 0;
- }
- /**
- * Process all devices and generate corresponding mtdparts string describing
- * all partitions on all devices.
- *
- * @param buf output buffer holding generated mtdparts string (output)
- * @param buflen buffer size
- * @return 0 on success, 1 otherwise
- */
- static int generate_mtdparts(char *buf, u32 buflen)
- {
- struct list_head *pentry, *dentry;
- struct mtd_device *dev;
- struct part_info *part, *prev_part;
- char *p = buf;
- char tmpbuf[32];
- u32 size, offset, len, part_cnt;
- u32 maxlen = buflen - 1;
- debug("--- generate_mtdparts ---\n");
- if (list_empty(&devices)) {
- buf[0] = '\0';
- return 0;
- }
- sprintf(p, "mtdparts=");
- p += 9;
- list_for_each(dentry, &devices) {
- dev = list_entry(dentry, struct mtd_device, link);
- /* copy mtd_id */
- len = strlen(dev->id->mtd_id) + 1;
- if (len > maxlen)
- goto cleanup;
- memcpy(p, dev->id->mtd_id, len - 1);
- p += len - 1;
- *(p++) = ':';
- maxlen -= len;
- /* format partitions */
- prev_part = NULL;
- part_cnt = 0;
- list_for_each(pentry, &dev->parts) {
- part = list_entry(pentry, struct part_info, link);
- size = part->size;
- offset = part->offset;
- part_cnt++;
- /* partition size */
- memsize_format(tmpbuf, size);
- len = strlen(tmpbuf);
- if (len > maxlen)
- goto cleanup;
- memcpy(p, tmpbuf, len);
- p += len;
- maxlen -= len;
- /* add offset only when there is a gap between
- * partitions */
- if ((!prev_part && (offset != 0)) ||
- (prev_part && ((prev_part->offset + prev_part->size) != part->offset))) {
- memsize_format(tmpbuf, offset);
- len = strlen(tmpbuf) + 1;
- if (len > maxlen)
- goto cleanup;
- *(p++) = '@';
- memcpy(p, tmpbuf, len - 1);
- p += len - 1;
- maxlen -= len;
- }
- /* copy name only if user supplied */
- if(!part->auto_name) {
- len = strlen(part->name) + 2;
- if (len > maxlen)
- goto cleanup;
- *(p++) = '(';
- memcpy(p, part->name, len - 2);
- p += len - 2;
- *(p++) = ')';
- maxlen -= len;
- }
- /* ro mask flag */
- if (part->mask_flags && MTD_WRITEABLE_CMD) {
- len = 2;
- if (len > maxlen)
- goto cleanup;
- *(p++) = 'r';
- *(p++) = 'o';
- maxlen -= 2;
- }
- /* print ',' separator if there are other partitions
- * following */
- if (dev->num_parts > part_cnt) {
- if (1 > maxlen)
- goto cleanup;
- *(p++) = ',';
- maxlen--;
- }
- prev_part = part;
- }
- /* print ';' separator if there are other devices following */
- if (dentry->next != &devices) {
- if (1 > maxlen)
- goto cleanup;
- *(p++) = ';';
- maxlen--;
- }
- }
- /* we still have at least one char left, as we decremented maxlen at
- * the begining */
- *p = '\0';
- return 0;
- cleanup:
- last_parts[0] = '\0';
- return 1;
- }
- /**
- * Call generate_mtdparts to process all devices and generate corresponding
- * mtdparts string, save it in mtdparts environment variable.
- *
- * @param buf output buffer holding generated mtdparts string (output)
- * @param buflen buffer size
- * @return 0 on success, 1 otherwise
- */
- static int generate_mtdparts_save(char *buf, u32 buflen)
- {
- int ret;
- ret = generate_mtdparts(buf, buflen);
- if ((buf[0] != '\0') && (ret == 0))
- setenv("mtdparts", buf);
- else
- setenv("mtdparts", NULL);
- return ret;
- }
- #if defined(CONFIG_CMD_MTDPARTS_SHOW_NET_SIZES)
- /**
- * Get the net size (w/o bad blocks) of the given partition.
- *
- * @param mtd the mtd info
- * @param part the partition
- * @return the calculated net size of this partition
- */
- static uint64_t net_part_size(struct mtd_info *mtd, struct part_info *part)
- {
- uint64_t i, net_size = 0;
- if (!mtd->block_isbad)
- return part->size;
- for (i = 0; i < part->size; i += mtd->erasesize) {
- if (!mtd->block_isbad(mtd, part->offset + i))
- net_size += mtd->erasesize;
- }
- return net_size;
- }
- #endif
- static void print_partition_table(void)
- {
- struct list_head *dentry, *pentry;
- struct part_info *part;
- struct mtd_device *dev;
- int part_num;
- list_for_each(dentry, &devices) {
- dev = list_entry(dentry, struct mtd_device, link);
- /* list partitions for given device */
- part_num = 0;
- #if defined(CONFIG_CMD_MTDPARTS_SHOW_NET_SIZES)
- struct mtd_info *mtd;
- if (get_mtd_info(dev->id->type, dev->id->num, &mtd))
- return;
- printf("\ndevice %s%d <%s>, # parts = %d\n",
- MTD_DEV_TYPE(dev->id->type), dev->id->num,
- dev->id->mtd_id, dev->num_parts);
- printf(" #: name\t\tsize\t\tnet size\toffset\t\tmask_flags\n");
- list_for_each(pentry, &dev->parts) {
- u32 net_size;
- char *size_note;
- part = list_entry(pentry, struct part_info, link);
- net_size = net_part_size(mtd, part);
- size_note = part->size == net_size ? " " : " (!)";
- printf("%2d: %-20s0x%08x\t0x%08x%s\t0x%08x\t%d\n",
- part_num, part->name, part->size,
- net_size, size_note, part->offset,
- part->mask_flags);
- #else /* !defined(CONFIG_CMD_MTDPARTS_SHOW_NET_SIZES) */
- printf("\ndevice %s%d <%s>, # parts = %d\n",
- MTD_DEV_TYPE(dev->id->type), dev->id->num,
- dev->id->mtd_id, dev->num_parts);
- printf(" #: name\t\tsize\t\toffset\t\tmask_flags\n");
- list_for_each(pentry, &dev->parts) {
- part = list_entry(pentry, struct part_info, link);
- printf("%2d: %-20s0x%08x\t0x%08x\t%d\n",
- part_num, part->name, part->size,
- part->offset, part->mask_flags);
- #endif /* defined(CONFIG_CMD_MTDPARTS_SHOW_NET_SIZES) */
- part_num++;
- }
- }
- if (list_empty(&devices))
- printf("no partitions defined\n");
- }
- /**
- * Format and print out a partition list for each device from global device
- * list.
- */
- static void list_partitions(void)
- {
- struct part_info *part;
- debug("\n---list_partitions---\n");
- print_partition_table();
- /* current_mtd_dev is not NULL only when we have non empty device list */
- if (current_mtd_dev) {
- part = mtd_part_info(current_mtd_dev, current_mtd_partnum);
- if (part) {
- printf("\nactive partition: %s%d,%d - (%s) 0x%08x @ 0x%08x\n",
- MTD_DEV_TYPE(current_mtd_dev->id->type),
- current_mtd_dev->id->num, current_mtd_partnum,
- part->name, part->size, part->offset);
- } else {
- printf("could not get current partition info\n\n");
- }
- }
- printf("\ndefaults:\n");
- printf("mtdids : %s\n",
- mtdids_default ? mtdids_default : "none");
- /*
- * Using printf() here results in printbuffer overflow
- * if default mtdparts string is greater than console
- * printbuffer. Use puts() to prevent system crashes.
- */
- puts("mtdparts: ");
- puts(mtdparts_default ? mtdparts_default : "none");
- puts("\n");
- }
- /**
- * Given partition identifier in form of <dev_type><dev_num>,<part_num> find
- * corresponding device and verify partition number.
- *
- * @param id string describing device and partition or partition name
- * @param dev pointer to the requested device (output)
- * @param part_num verified partition number (output)
- * @param part pointer to requested partition (output)
- * @return 0 on success, 1 otherwise
- */
- int find_dev_and_part(const char *id, struct mtd_device **dev,
- u8 *part_num, struct part_info **part)
- {
- struct list_head *dentry, *pentry;
- u8 type, dnum, pnum;
- const char *p;
- debug("--- find_dev_and_part ---\nid = %s\n", id);
- list_for_each(dentry, &devices) {
- *part_num = 0;
- *dev = list_entry(dentry, struct mtd_device, link);
- list_for_each(pentry, &(*dev)->parts) {
- *part = list_entry(pentry, struct part_info, link);
- if (strcmp((*part)->name, id) == 0)
- return 0;
- (*part_num)++;
- }
- }
- p = id;
- *dev = NULL;
- *part = NULL;
- *part_num = 0;
- if (mtd_id_parse(p, &p, &type, &dnum) != 0)
- return 1;
- if ((*p++ != ',') || (*p == '\0')) {
- printf("no partition number specified\n");
- return 1;
- }
- pnum = simple_strtoul(p, (char **)&p, 0);
- if (*p != '\0') {
- printf("unexpected trailing character '%c'\n", *p);
- return 1;
- }
- if ((*dev = device_find(type, dnum)) == NULL) {
- printf("no such device %s%d\n", MTD_DEV_TYPE(type), dnum);
- return 1;
- }
- if ((*part = mtd_part_info(*dev, pnum)) == NULL) {
- printf("no such partition\n");
- *dev = NULL;
- return 1;
- }
- *part_num = pnum;
- return 0;
- }
- /**
- * Find and delete partition. For partition id format see find_dev_and_part().
- *
- * @param id string describing device and partition
- * @return 0 on success, 1 otherwise
- */
- static int delete_partition(const char *id)
- {
- u8 pnum;
- struct mtd_device *dev;
- struct part_info *part;
- if (find_dev_and_part(id, &dev, &pnum, &part) == 0) {
- debug("delete_partition: device = %s%d, partition %d = (%s) 0x%08x@0x%08x\n",
- MTD_DEV_TYPE(dev->id->type), dev->id->num, pnum,
- part->name, part->size, part->offset);
- if (part_del(dev, part) != 0)
- return 1;
- if (generate_mtdparts_save(last_parts, MTDPARTS_MAXLEN) != 0) {
- printf("generated mtdparts too long, resetting to null\n");
- return 1;
- }
- return 0;
- }
- printf("partition %s not found\n", id);
- return 1;
- }
- #if defined(CONFIG_CMD_MTDPARTS_SPREAD)
- /**
- * Increase the size of the given partition so that it's net size is at least
- * as large as the size member and such that the next partition would start on a
- * good block if it were adjacent to this partition.
- *
- * @param mtd the mtd device
- * @param part the partition
- * @param next_offset pointer to the offset of the next partition after this
- * partition's size has been modified (output)
- */
- static void spread_partition(struct mtd_info *mtd, struct part_info *part,
- uint64_t *next_offset)
- {
- uint64_t net_size, padding_size = 0;
- int truncated;
- mtd_get_len_incl_bad(mtd, part->offset, part->size, &net_size,
- &truncated);
- /*
- * Absorb bad blocks immediately following this
- * partition also into the partition, such that
- * the next partition starts with a good block.
- */
- if (!truncated) {
- mtd_get_len_incl_bad(mtd, part->offset + net_size,
- mtd->erasesize, &padding_size, &truncated);
- if (truncated)
- padding_size = 0;
- else
- padding_size -= mtd->erasesize;
- }
- if (truncated) {
- printf("truncated partition %s to %lld bytes\n", part->name,
- (uint64_t) net_size + padding_size);
- }
- part->size = net_size + padding_size;
- *next_offset = part->offset + part->size;
- }
- /**
- * Adjust all of the partition sizes, such that all partitions are at least
- * as big as their mtdparts environment variable sizes and they each start
- * on a good block.
- *
- * @return 0 on success, 1 otherwise
- */
- static int spread_partitions(void)
- {
- struct list_head *dentry, *pentry;
- struct mtd_device *dev;
- struct part_info *part;
- struct mtd_info *mtd;
- int part_num;
- uint64_t cur_offs;
- list_for_each(dentry, &devices) {
- dev = list_entry(dentry, struct mtd_device, link);
- if (get_mtd_info(dev->id->type, dev->id->num, &mtd))
- return 1;
- part_num = 0;
- cur_offs = 0;
- list_for_each(pentry, &dev->parts) {
- part = list_entry(pentry, struct part_info, link);
- debug("spread_partitions: device = %s%d, partition %d ="
- " (%s) 0x%08x@0x%08x\n",
- MTD_DEV_TYPE(dev->id->type), dev->id->num,
- part_num, part->name, part->size,
- part->offset);
- if (cur_offs > part->offset)
- part->offset = cur_offs;
- spread_partition(mtd, part, &cur_offs);
- part_num++;
- }
- }
- index_partitions();
- if (generate_mtdparts_save(last_parts, MTDPARTS_MAXLEN) != 0) {
- printf("generated mtdparts too long, resetting to null\n");
- return 1;
- }
- return 0;
- }
- #endif /* CONFIG_CMD_MTDPARTS_SPREAD */
- /**
- * Accept character string describing mtd partitions and call device_parse()
- * for each entry. Add created devices to the global devices list.
- *
- * @param mtdparts string specifing mtd partitions
- * @return 0 on success, 1 otherwise
- */
- static int parse_mtdparts(const char *const mtdparts)
- {
- const char *p = mtdparts;
- struct mtd_device *dev;
- int err = 1;
- debug("\n---parse_mtdparts---\nmtdparts = %s\n\n", p);
- /* delete all devices and partitions */
- if (mtd_devices_init() != 0) {
- printf("could not initialise device list\n");
- return err;
- }
- /* re-read 'mtdparts' variable, mtd_devices_init may be updating env */
- p = getenv("mtdparts");
- if (strncmp(p, "mtdparts=", 9) != 0) {
- printf("mtdparts variable doesn't start with 'mtdparts='\n");
- return err;
- }
- p += 9;
- while (p && (*p != '\0')) {
- err = 1;
- if ((device_parse(p, &p, &dev) != 0) || (!dev))
- break;
- debug("+ device: %s\t%d\t%s\n", MTD_DEV_TYPE(dev->id->type),
- dev->id->num, dev->id->mtd_id);
- /* check if parsed device is already on the list */
- if (device_find(dev->id->type, dev->id->num) != NULL) {
- printf("device %s%d redefined, please correct mtdparts variable\n",
- MTD_DEV_TYPE(dev->id->type), dev->id->num);
- break;
- }
- list_add_tail(&dev->link, &devices);
- err = 0;
- }
- if (err == 1) {
- device_delall(&devices);
- return 1;
- }
- return 0;
- }
- /**
- * Parse provided string describing mtdids mapping (see file header for mtdids
- * variable format). Allocate memory for each entry and add all found entries
- * to the global mtdids list.
- *
- * @param ids mapping string
- * @return 0 on success, 1 otherwise
- */
- static int parse_mtdids(const char *const ids)
- {
- const char *p = ids;
- const char *mtd_id;
- int mtd_id_len;
- struct mtdids *id;
- struct list_head *entry, *n;
- struct mtdids *id_tmp;
- u8 type, num;
- u32 size;
- int ret = 1;
- debug("\n---parse_mtdids---\nmtdids = %s\n\n", ids);
- /* clean global mtdids list */
- list_for_each_safe(entry, n, &mtdids) {
- id_tmp = list_entry(entry, struct mtdids, link);
- debug("mtdids del: %d %d\n", id_tmp->type, id_tmp->num);
- list_del(entry);
- free(id_tmp);
- }
- last_ids[0] = '\0';
- INIT_LIST_HEAD(&mtdids);
- while(p && (*p != '\0')) {
- ret = 1;
- /* parse 'nor'|'nand'|'onenand'<dev-num> */
- if (mtd_id_parse(p, &p, &type, &num) != 0)
- break;
- if (*p != '=') {
- printf("mtdids: incorrect <dev-num>\n");
- break;
- }
- p++;
- /* check if requested device exists */
- if (mtd_device_validate(type, num, &size) != 0)
- return 1;
- /* locate <mtd-id> */
- mtd_id = p;
- if ((p = strchr(mtd_id, ',')) != NULL) {
- mtd_id_len = p - mtd_id + 1;
- p++;
- } else {
- mtd_id_len = strlen(mtd_id) + 1;
- }
- if (mtd_id_len == 0) {
- printf("mtdids: no <mtd-id> identifier\n");
- break;
- }
- /* check if this id is already on the list */
- int double_entry = 0;
- list_for_each(entry, &mtdids) {
- id_tmp = list_entry(entry, struct mtdids, link);
- if ((id_tmp->type == type) && (id_tmp->num == num)) {
- double_entry = 1;
- break;
- }
- }
- if (double_entry) {
- printf("device id %s%d redefined, please correct mtdids variable\n",
- MTD_DEV_TYPE(type), num);
- break;
- }
- /* allocate mtdids structure */
- if (!(id = (struct mtdids *)malloc(sizeof(struct mtdids) + mtd_id_len))) {
- printf("out of memory\n");
- break;
- }
- memset(id, 0, sizeof(struct mtdids) + mtd_id_len);
- id->num = num;
- id->type = type;
- id->size = size;
- id->mtd_id = (char *)(id + 1);
- strncpy(id->mtd_id, mtd_id, mtd_id_len - 1);
- id->mtd_id[mtd_id_len - 1] = '\0';
- INIT_LIST_HEAD(&id->link);
- debug("+ id %s%d\t%16d bytes\t%s\n",
- MTD_DEV_TYPE(id->type), id->num,
- id->size, id->mtd_id);
- list_add_tail(&id->link, &mtdids);
- ret = 0;
- }
- if (ret == 1) {
- /* clean mtdids list and free allocated memory */
- list_for_each_safe(entry, n, &mtdids) {
- id_tmp = list_entry(entry, struct mtdids, link);
- list_del(entry);
- free(id_tmp);
- }
- return 1;
- }
- return 0;
- }
- /**
- * Parse and initialize global mtdids mapping and create global
- * device/partition list.
- *
- * @return 0 on success, 1 otherwise
- */
- int mtdparts_init(void)
- {
- static int initialized = 0;
- const char *ids, *parts;
- const char *current_partition;
- int ids_changed;
- char tmp_ep[PARTITION_MAXLEN];
- debug("\n---mtdparts_init---\n");
- if (!initialized) {
- INIT_LIST_HEAD(&mtdids);
- INIT_LIST_HEAD(&devices);
- memset(last_ids, 0, MTDIDS_MAXLEN);
- memset(last_parts, 0, MTDPARTS_MAXLEN);
- memset(last_partition, 0, PARTITION_MAXLEN);
- initialized = 1;
- }
- /* get variables */
- ids = getenv("mtdids");
- parts = getenv("mtdparts");
- current_partition = getenv("partition");
- /* save it for later parsing, cannot rely on current partition pointer
- * as 'partition' variable may be updated during init */
- tmp_ep[0] = '\0';
- if (current_partition)
- strncpy(tmp_ep, current_partition, PARTITION_MAXLEN);
- debug("last_ids : %s\n", last_ids);
- debug("env_ids : %s\n", ids);
- debug("last_parts: %s\n", last_parts);
- debug("env_parts : %s\n\n", parts);
- debug("last_partition : %s\n", last_partition);
- debug("env_partition : %s\n", current_partition);
- /* if mtdids varible is empty try to use defaults */
- if (!ids) {
- if (mtdids_default) {
- debug("mtdids variable not defined, using default\n");
- ids = mtdids_default;
- setenv("mtdids", (char *)ids);
- } else {
- printf("mtdids not defined, no default present\n");
- return 1;
- }
- }
- if (strlen(ids) > MTDIDS_MAXLEN - 1) {
- printf("mtdids too long (> %d)\n", MTDIDS_MAXLEN);
- return 1;
- }
- /* do no try to use defaults when mtdparts variable is not defined,
- * just check the length */
- if (!parts)
- printf("mtdparts variable not set, see 'help mtdparts'\n");
- if (parts && (strlen(parts) > MTDPARTS_MAXLEN - 1)) {
- printf("mtdparts too long (> %d)\n", MTDPARTS_MAXLEN);
- return 1;
- }
- /* check if we have already parsed those mtdids */
- if ((last_ids[0] != '\0') && (strcmp(last_ids, ids) == 0)) {
- ids_changed = 0;
- } else {
- ids_changed = 1;
- if (parse_mtdids(ids) != 0) {
- mtd_devices_init();
- return 1;
- }
- /* ok it's good, save new ids */
- strncpy(last_ids, ids, MTDIDS_MAXLEN);
- }
- /* parse partitions if either mtdparts or mtdids were updated */
- if (parts && ((last_parts[0] == '\0') || ((strcmp(last_parts, parts) != 0)) || ids_changed)) {
- if (parse_mtdparts(parts) != 0)
- return 1;
- if (list_empty(&devices)) {
- printf("mtdparts_init: no valid partitions\n");
- return 1;
- }
- /* ok it's good, save new parts */
- strncpy(last_parts, parts, MTDPARTS_MAXLEN);
- /* reset first partition from first dev from the list as current */
- current_mtd_dev = list_entry(devices.next, struct mtd_device, link);
- current_mtd_partnum = 0;
- current_save();
- debug("mtdparts_init: current_mtd_dev = %s%d, current_mtd_partnum = %d\n",
- MTD_DEV_TYPE(current_mtd_dev->id->type),
- current_mtd_dev->id->num, current_mtd_partnum);
- }
- /* mtdparts variable was reset to NULL, delete all devices/partitions */
- if (!parts && (last_parts[0] != '\0'))
- return mtd_devices_init();
- /* do not process current partition if mtdparts variable is null */
- if (!parts)
- return 0;
- /* is current partition set in environment? if so, use it */
- if ((tmp_ep[0] != '\0') && (strcmp(tmp_ep, last_partition) != 0)) {
- struct part_info *p;
- struct mtd_device *cdev;
- u8 pnum;
- debug("--- getting current partition: %s\n", tmp_ep);
- if (find_dev_and_part(tmp_ep, &cdev, &pnum, &p) == 0) {
- current_mtd_dev = cdev;
- current_mtd_partnum = pnum;
- current_save();
- }
- } else if (getenv("partition") == NULL) {
- debug("no partition variable set, setting...\n");
- current_save();
- }
- return 0;
- }
- /**
- * Return pointer to the partition of a requested number from a requested
- * device.
- *
- * @param dev device that is to be searched for a partition
- * @param part_num requested partition number
- * @return pointer to the part_info, NULL otherwise
- */
- static struct part_info* mtd_part_info(struct mtd_device *dev, unsigned int part_num)
- {
- struct list_head *entry;
- struct part_info *part;
- int num;
- if (!dev)
- return NULL;
- debug("\n--- mtd_part_info: partition number %d for device %s%d (%s)\n",
- part_num, MTD_DEV_TYPE(dev->id->type),
- dev->id->num, dev->id->mtd_id);
- if (part_num >= dev->num_parts) {
- printf("invalid partition number %d for device %s%d (%s)\n",
- part_num, MTD_DEV_TYPE(dev->id->type),
- dev->id->num, dev->id->mtd_id);
- return NULL;
- }
- /* locate partition number, return it */
- num = 0;
- list_for_each(entry, &dev->parts) {
- part = list_entry(entry, struct part_info, link);
- if (part_num == num++) {
- return part;
- }
- }
- return NULL;
- }
- /***************************************************/
- /* U-boot commands */
- /***************************************************/
- /* command line only */
- /**
- * Routine implementing u-boot chpart command. Sets new current partition based
- * on the user supplied partition id. For partition id format see find_dev_and_part().
- *
- * @param cmdtp command internal data
- * @param flag command flag
- * @param argc number of arguments supplied to the command
- * @param argv arguments list
- * @return 0 on success, 1 otherwise
- */
- static int do_chpart(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
- {
- /* command line only */
- struct mtd_device *dev;
- struct part_info *part;
- u8 pnum;
- if (mtdparts_init() !=0)
- return 1;
- if (argc < 2) {
- printf("no partition id specified\n");
- return 1;
- }
- if (find_dev_and_part(argv[1], &dev, &pnum, &part) != 0)
- return 1;
- current_mtd_dev = dev;
- current_mtd_partnum = pnum;
- current_save();
- printf("partition changed to %s%d,%d\n",
- MTD_DEV_TYPE(dev->id->type), dev->id->num, pnum);
- return 0;
- }
- /**
- * Routine implementing u-boot mtdparts command. Initialize/update default global
- * partition list and process user partition request (list, add, del).
- *
- * @param cmdtp command internal data
- * @param flag command flag
- * @param argc number of arguments supplied to the command
- * @param argv arguments list
- * @return 0 on success, 1 otherwise
- */
- static int do_mtdparts(cmd_tbl_t *cmdtp, int flag, int argc,
- char * const argv[])
- {
- if (argc == 2) {
- if (strcmp(argv[1], "default") == 0) {
- setenv("mtdids", (char *)mtdids_default);
- setenv("mtdparts", (char *)mtdparts_default);
- setenv("partition", NULL);
- mtdparts_init();
- return 0;
- } else if (strcmp(argv[1], "delall") == 0) {
- /* this may be the first run, initialize lists if needed */
- mtdparts_init();
- setenv("mtdparts", NULL);
- /* mtd_devices_init() calls current_save() */
- return mtd_devices_init();
- }
- }
- /* make sure we are in sync with env variables */
- if (mtdparts_init() != 0)
- return 1;
- if (argc == 1) {
- list_partitions();
- return 0;
- }
- /* mtdparts add <mtd-dev> <size>[@<offset>] <name> [ro] */
- if (((argc == 5) || (argc == 6)) && (strncmp(argv[1], "add", 3) == 0)) {
- #define PART_ADD_DESC_MAXLEN 64
- char tmpbuf[PART_ADD_DESC_MAXLEN];
- #if defined(CONFIG_CMD_MTDPARTS_SPREAD)
- struct mtd_info *mtd;
- uint64_t next_offset;
- #endif
- u8 type, num, len;
- struct mtd_device *dev;
- struct mtd_device *dev_tmp;
- struct mtdids *id;
- struct part_info *p;
- if (mtd_id_parse(argv[2], NULL, &type, &num) != 0)
- return 1;
- if ((id = id_find(type, num)) == NULL) {
- printf("no such device %s defined in mtdids variable\n", argv[2]);
- return 1;
- }
- len = strlen(id->mtd_id) + 1; /* 'mtd_id:' */
- len += strlen(argv[3]); /* size@offset */
- len += strlen(argv[4]) + 2; /* '(' name ')' */
- if (argv[5] && (strlen(argv[5]) == 2))
- len += 2; /* 'ro' */
- if (len >= PART_ADD_DESC_MAXLEN) {
- printf("too long partition description\n");
- return 1;
- }
- sprintf(tmpbuf, "%s:%s(%s)%s",
- id->mtd_id, argv[3], argv[4], argv[5] ? argv[5] : "");
- debug("add tmpbuf: %s\n", tmpbuf);
- if ((device_parse(tmpbuf, NULL, &dev) != 0) || (!dev))
- return 1;
- debug("+ %s\t%d\t%s\n", MTD_DEV_TYPE(dev->id->type),
- dev->id->num, dev->id->mtd_id);
- p = list_entry(dev->parts.next, struct part_info, link);
- #if defined(CONFIG_CMD_MTDPARTS_SPREAD)
- if (get_mtd_info(dev->id->type, dev->id->num, &mtd))
- return 1;
- if (!strcmp(&argv[1][3], ".spread")) {
- spread_partition(mtd, p, &next_offset);
- debug("increased %s to %d bytes\n", p->name, p->size);
- }
- #endif
- dev_tmp = device_find(dev->id->type, dev->id->num);
- if (dev_tmp == NULL) {
- device_add(dev);
- } else if (part_add(dev_tmp, p) != 0) {
- /* merge new partition with existing ones*/
- device_del(dev);
- return 1;
- }
- if (generate_mtdparts_save(last_parts, MTDPARTS_MAXLEN) != 0) {
- printf("generated mtdparts too long, resetting to null\n");
- return 1;
- }
- return 0;
- }
- /* mtdparts del part-id */
- if ((argc == 3) && (strcmp(argv[1], "del") == 0)) {
- debug("del: part-id = %s\n", argv[2]);
- return delete_partition(argv[2]);
- }
- #if defined(CONFIG_CMD_MTDPARTS_SPREAD)
- if ((argc == 2) && (strcmp(argv[1], "spread") == 0))
- return spread_partitions();
- #endif /* CONFIG_CMD_MTDPARTS_SPREAD */
- return CMD_RET_USAGE;
- }
- /***************************************************/
- U_BOOT_CMD(
- chpart, 2, 0, do_chpart,
- "change active partition",
- "part-id\n"
- " - change active partition (e.g. part-id = nand0,1)"
- );
- #ifdef CONFIG_SYS_LONGHELP
- static char mtdparts_help_text[] =
- "\n"
- " - list partition table\n"
- "mtdparts delall\n"
- " - delete all partitions\n"
- "mtdparts del part-id\n"
- " - delete partition (e.g. part-id = nand0,1)\n"
- "mtdparts add <mtd-dev> <size>[@<offset>] [<name>] [ro]\n"
- " - add partition\n"
- #if defined(CONFIG_CMD_MTDPARTS_SPREAD)
- "mtdparts add.spread <mtd-dev> <size>[@<offset>] [<name>] [ro]\n"
- " - add partition, padding size by skipping bad blocks\n"
- #endif
- "mtdparts default\n"
- " - reset partition table to defaults\n"
- #if defined(CONFIG_CMD_MTDPARTS_SPREAD)
- "mtdparts spread\n"
- " - adjust the sizes of the partitions so they are\n"
- " at least as big as the mtdparts variable specifies\n"
- " and they each start on a good block\n\n"
- #else
- "\n"
- #endif /* CONFIG_CMD_MTDPARTS_SPREAD */
- "-----\n\n"
- "this command uses three environment variables:\n\n"
- "'partition' - keeps current partition identifier\n\n"
- "partition := <part-id>\n"
- "<part-id> := <dev-id>,part_num\n\n"
- "'mtdids' - linux kernel mtd device id <-> u-boot device id mapping\n\n"
- "mtdids=<idmap>[,<idmap>,...]\n\n"
- "<idmap> := <dev-id>=<mtd-id>\n"
- "<dev-id> := 'nand'|'nor'|'onenand'<dev-num>\n"
- "<dev-num> := mtd device number, 0...\n"
- "<mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)\n\n"
- "'mtdparts' - partition list\n\n"
- "mtdparts=mtdparts=<mtd-def>[;<mtd-def>...]\n\n"
- "<mtd-def> := <mtd-id>:<part-def>[,<part-def>...]\n"
- "<mtd-id> := unique device tag used by linux kernel to find mtd device (mtd->name)\n"
- "<part-def> := <size>[@<offset>][<name>][<ro-flag>]\n"
- "<size> := standard linux memsize OR '-' to denote all remaining space\n"
- "<offset> := partition start offset within the device\n"
- "<name> := '(' NAME ')'\n"
- "<ro-flag> := when set to 'ro' makes partition read-only (not used, passed to kernel)";
- #endif
- U_BOOT_CMD(
- mtdparts, 6, 0, do_mtdparts,
- "define flash/nand partitions", mtdparts_help_text
- );
- /***************************************************/
|