Newer
Older
else
offset = part->offset;
/* verify alignment and size */
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
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;
}
DEBUGF("\ntotal partitions: %d\n", num_parts);
/* check for next device presence */
if (p) {
if (*p == ';') {
*ret = ++p;
} else if (*p == '\0') {
*ret = p;
} else {
printf("unexpected character '%c' at the end of device\n", *p);
*ret = NULL;
}
}
/* 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 */
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
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;
DEBUGF("===\n\n");
return 0;
}
/**
* Initialize global device list.
*
* @return 0 on success, 1 otherwise
*/
static int jffs2_devices_init(void)
{
last_parts[0] = '\0';
current_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;
DEBUGF("--- 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);
DEBUGF("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;
}
#endif /* #ifdef CONFIG_JFFS2_CMDLINE */
/**
* 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 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;
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
} 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;
}
#ifdef CONFIG_JFFS2_CMDLINE
/**
* 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;
DEBUGF("--- 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);
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
/* 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;
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
/* 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) {
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
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;
}
/**
* Format and print out a partition list for each device from global device
* list.
*/
static void list_partitions(void)
{
struct list_head *dentry, *pentry;
struct part_info *part;
struct mtd_device *dev;
int part_num;
DEBUGF("\n---list_partitions---\n");
list_for_each(dentry, &devices) {
dev = list_entry(dentry, struct mtd_device, link);
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\t\tsize\t\toffset\t\tmask_flags\n");
/* list partitions for given device */
part_num = 0;
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);
part_num++;
}
}
if (list_empty(&devices))
printf("no partitions defined\n");
/* current_dev is not NULL only when we have non empty device list */
if (current_dev) {
part = jffs2_part_info(current_dev, current_partnum);
if (part) {
printf("\nactive partition: %s%d,%d - (%s) 0x%08x @ 0x%08x\n",
MTD_DEV_TYPE(current_dev->id->type),
current_dev->id->num, current_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);
printf("mtdparts: %s\n", mtdparts_default);
}
/**
* 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;
DEBUGF("--- 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 (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 = jffs2_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().
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
* @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) {
DEBUGF("delete_partition: device = %s%d, partition %d = (%s) 0x%08lx@0x%08lx\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, reseting to null\n");
return 1;
}
return 0;
}
printf("partition %s not found\n", id);
return 1;
}
/**
* 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;
DEBUGF("\n---parse_mtdparts---\nmtdparts = %s\n\n", p);
/* delete all devices and partitions */
if (jffs2_devices_init() != 0) {
printf("could not initialise device list\n");
return err;
}
/* re-read 'mtdparts' variable, jffs2_devices_init may be updating env */
p = getenv("mtdparts");
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
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;
DEBUGF("+ 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;
DEBUGF("\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);
DEBUGF("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> */
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
if (id_parse(p, &p, &type, &num) != 0)
break;
if (*p != '=') {
printf("mtdids: incorrect <dev-num>\n");
break;
}
p++;
/* check if requested device exists */
if (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);
DEBUGF("+ 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)
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
static int initialized = 0;
const char *ids, *parts;
const char *current_partition;
int ids_changed;
char tmp_ep[PARTITION_MAXLEN];
DEBUGF("\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);
DEBUGF("last_ids : %s\n", last_ids);
DEBUGF("env_ids : %s\n", ids);
DEBUGF("last_parts: %s\n", last_parts);
DEBUGF("env_parts : %s\n\n", parts);
DEBUGF("last_partition : %s\n", last_partition);
DEBUGF("env_partition : %s\n", current_partition);
/* if mtdids varible is empty try to use defaults */
if (!ids) {
if (mtdids_default) {
DEBUGF("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) {
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
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_dev = list_entry(devices.next, struct mtd_device, link);
current_partnum = 0;
current_save();
DEBUGF("mtdparts_init: current_dev = %s%d, current_partnum = %d\n",
MTD_DEV_TYPE(current_dev->id->type),
current_dev->id->num, current_partnum);
}
/* mtdparts variable was reset to NULL, delete all devices/partitions */
if (!parts && (last_parts[0] != '\0'))
return jffs2_devices_init();
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
/* 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;
DEBUGF("--- getting current partition: %s\n", tmp_ep);
if (find_dev_and_part(tmp_ep, &cdev, &pnum, &p) == 0) {
current_dev = cdev;
current_partnum = pnum;
current_save();
}
} else if (getenv("partition") == NULL) {
DEBUGF("no partition variable set, setting...\n");
current_save();
}
return 0;
}
#else /* #ifdef CONFIG_JFFS2_CMDLINE */
/*
* 'Static' version of command line mtdparts_init() routine. Single partition on
* a single device configuration.
*/
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
/**
* Calculate sector size.
*
* @return sector size
*/
static inline u32 get_part_sector_size_nand(struct mtdids *id)
{
#if defined(CONFIG_JFFS2_NAND) && defined(CONFIG_CMD_NAND)
#if defined(CONFIG_NAND_LEGACY)
extern struct nand_chip nand_dev_desc[CONFIG_SYS_MAX_NAND_DEVICE];
return nand_dev_desc[id->num].erasesize;
#else
nand_info_t *nand;
nand = &nand_info[id->num];
return nand->erasesize;
#endif
#else
BUG();
return 0;
#endif
}
static inline u32 get_part_sector_size_nor(struct mtdids *id, struct part_info *part)
{
#if defined(CONFIG_CMD_FLASH)
extern flash_info_t flash_info[];
u32 end_phys, start_phys, sector_size = 0, size = 0;
int i;
flash_info_t *flash;
flash = &flash_info[id->num];
start_phys = flash->start[0] + part->offset;
end_phys = start_phys + part->size;
for (i = 0; i < flash->sector_count; i++) {
if (flash->start[i] >= end_phys)
break;
if (flash->start[i] >= start_phys) {
if (i == flash->sector_count - 1) {
size = flash->start[0] + flash->size - flash->start[i];
} else {
size = flash->start[i+1] - flash->start[i];
}
if (sector_size < size)
sector_size = size;
}
}
return sector_size;
#else
BUG();
return 0;
#endif
}
static inline u32 get_part_sector_size_onenand(void)
{
#if defined(CONFIG_CMD_ONENAND)
struct mtd_info *mtd;
mtd = &onenand_mtd;
return mtd->erasesize;
#else
BUG();
return 0;
#endif
}
static inline u32 get_part_sector_size(struct mtdids *id, struct part_info *part)
{
if (id->type == MTD_DEV_TYPE_NAND)
return get_part_sector_size_nand(id);
else if (id->type == MTD_DEV_TYPE_NOR)
return get_part_sector_size_nor(id, part);
else if (id->type == MTD_DEV_TYPE_ONENAND)
return get_part_sector_size_onenand();
else
DEBUGF("Error: Unknown device type.\n");
return 0;
}
/**
* Parse and initialize global mtdids mapping and create global
*
* @return 0 on success, 1 otherwise
*/
int mtdparts_init(void)
{
static int initialized = 0;
u32 size;
char *dev_name;
DEBUGF("\n---mtdparts_init---\n");
if (!initialized) {
struct mtdids *id;
struct part_info *part;
initialized = 1;
current_dev = (struct mtd_device *)
malloc(sizeof(struct mtd_device) +
sizeof(struct part_info) +
sizeof(struct mtdids));
if (!current_dev) {
printf("out of memory\n");
return 1;
}
memset(current_dev, 0, sizeof(struct mtd_device) +
sizeof(struct part_info) + sizeof(struct mtdids));
id = (struct mtdids *)(current_dev + 1);
part = (struct part_info *)(id + 1);
/* id */
id->mtd_id = "single part";
#if defined(CONFIG_JFFS2_DEV)
dev_name = CONFIG_JFFS2_DEV;
dev_name = "nor0";
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
if ((id_parse(dev_name, NULL, &id->type, &id->num) != 0) ||
(device_validate(id->type, id->num, &size) != 0)) {
printf("incorrect device: %s%d\n", MTD_DEV_TYPE(id->type), id->num);
free(current_dev);
return 1;
}
id->size = size;
INIT_LIST_HEAD(&id->link);
DEBUGF("dev id: type = %d, num = %d, size = 0x%08lx, mtd_id = %s\n",
id->type, id->num, id->size, id->mtd_id);
/* partition */
part->name = "static";
part->auto_name = 0;
#if defined(CONFIG_JFFS2_PART_SIZE)
part->size = CONFIG_JFFS2_PART_SIZE;
#else
part->size = SIZE_REMAINING;
#endif
#if defined(CONFIG_JFFS2_PART_OFFSET)
part->offset = CONFIG_JFFS2_PART_OFFSET;
#else
part->offset = 0x00000000;
part->sector_size = get_part_sector_size(id, part);
part->dev = current_dev;
INIT_LIST_HEAD(&part->link);
/* recalculate size if needed */
if (part->size == SIZE_REMAINING)
part->size = id->size - part->offset;
DEBUGF("part : name = %s, size = 0x%08lx, offset = 0x%08lx\n",
part->name, part->size, part->offset);
/* device */
current_dev->id = id;
INIT_LIST_HEAD(¤t_dev->link);
current_dev->num_parts = 1;
INIT_LIST_HEAD(¤t_dev->parts);
list_add(&part->link, ¤t_dev->parts);
#endif /* #ifdef CONFIG_JFFS2_CMDLINE */
/**
* 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* jffs2_part_info(struct mtd_device *dev, unsigned int part_num)
struct list_head *entry;
struct part_info *part;
int num;
if (!dev)
return NULL;
DEBUGF("\n--- jffs2_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;
}