Commit 8fdf1e0f6d645f79a9b0677e062bf48d786c5b9e

Authored by Vipin Kumar
Committed by Scott Wood
1 parent 47104c37de

imls: Add support to list images in NAND device

This patch adds support to list images in NAND flash through imls

Signed-off-by: Vipin Kumar <vipin.kumar@st.com>

Showing 2 changed files with 166 additions and 4 deletions Side-by-side Diff

... ... @@ -842,7 +842,8 @@
842 842 CONFIG_CMD_I2C * I2C serial bus support
843 843 CONFIG_CMD_IDE * IDE harddisk support
844 844 CONFIG_CMD_IMI iminfo
845   - CONFIG_CMD_IMLS List all found images
  845 + CONFIG_CMD_IMLS List all images found in NOR flash
  846 + CONFIG_CMD_IMLS_NAND List all images found in NAND flash
846 847 CONFIG_CMD_IMMAP * IMMR dump support
847 848 CONFIG_CMD_IMPORTENV * import an environment
848 849 CONFIG_CMD_INI * import data from an ini file into the env
... ... @@ -79,9 +79,15 @@
79 79 #include <flash.h>
80 80 #include <mtd/cfi_flash.h>
81 81 extern flash_info_t flash_info[]; /* info for FLASH chips */
  82 +#endif
  83 +
  84 +#if defined(CONFIG_CMD_IMLS) || defined(CONFIG_CMD_IMLS_NAND)
82 85 static int do_imls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
83 86 #endif
84 87  
  88 +#include <linux/err.h>
  89 +#include <nand.h>
  90 +
85 91 #ifdef CONFIG_SILENT_CONSOLE
86 92 static void fixup_silent_linux(void);
87 93 #endif
... ... @@ -1192,7 +1198,7 @@
1192 1198 /* imls - list all images found in flash */
1193 1199 /*******************************************************************/
1194 1200 #if defined(CONFIG_CMD_IMLS)
1195   -static int do_imls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  1201 +static int do_imls_nor(void)
1196 1202 {
1197 1203 flash_info_t *info;
1198 1204 int i, j;
1199 1205  
... ... @@ -1241,7 +1247,162 @@
1241 1247 }
1242 1248 next_bank: ;
1243 1249 }
  1250 + return 0;
  1251 +}
  1252 +#endif
1244 1253  
  1254 +#if defined(CONFIG_CMD_IMLS_NAND)
  1255 +static int nand_imls_legacyimage(nand_info_t *nand, int nand_dev, loff_t off,
  1256 + size_t len)
  1257 +{
  1258 + void *imgdata;
  1259 + int ret;
  1260 +
  1261 + imgdata = malloc(len);
  1262 + if (!imgdata) {
  1263 + printf("May be a Legacy Image at NAND device %d offset %08llX:\n",
  1264 + nand_dev, off);
  1265 + printf(" Low memory(cannot allocate memory for image)\n");
  1266 + return -ENOMEM;
  1267 + }
  1268 +
  1269 + ret = nand_read_skip_bad(nand, off, &len,
  1270 + imgdata);
  1271 + if (ret < 0 && ret != -EUCLEAN) {
  1272 + free(imgdata);
  1273 + return ret;
  1274 + }
  1275 +
  1276 + if (!image_check_hcrc(imgdata)) {
  1277 + free(imgdata);
  1278 + return 0;
  1279 + }
  1280 +
  1281 + printf("Legacy Image at NAND device %d offset %08llX:\n",
  1282 + nand_dev, off);
  1283 + image_print_contents(imgdata);
  1284 +
  1285 + puts(" Verifying Checksum ... ");
  1286 + if (!image_check_dcrc(imgdata))
  1287 + puts("Bad Data CRC\n");
  1288 + else
  1289 + puts("OK\n");
  1290 +
  1291 + free(imgdata);
  1292 +
  1293 + return 0;
  1294 +}
  1295 +
  1296 +static int nand_imls_fitimage(nand_info_t *nand, int nand_dev, loff_t off,
  1297 + size_t len)
  1298 +{
  1299 + void *imgdata;
  1300 + int ret;
  1301 +
  1302 + imgdata = malloc(len);
  1303 + if (!imgdata) {
  1304 + printf("May be a FIT Image at NAND device %d offset %08llX:\n",
  1305 + nand_dev, off);
  1306 + printf(" Low memory(cannot allocate memory for image)\n");
  1307 + return -ENOMEM;
  1308 + }
  1309 +
  1310 + ret = nand_read_skip_bad(nand, off, &len,
  1311 + imgdata);
  1312 + if (ret < 0 && ret != -EUCLEAN) {
  1313 + free(imgdata);
  1314 + return ret;
  1315 + }
  1316 +
  1317 + if (!fit_check_format(imgdata)) {
  1318 + free(imgdata);
  1319 + return 0;
  1320 + }
  1321 +
  1322 + printf("FIT Image at NAND device %d offset %08llX:\n", nand_dev, off);
  1323 +
  1324 + fit_print_contents(imgdata);
  1325 + free(imgdata);
  1326 +
  1327 + return 0;
  1328 +}
  1329 +
  1330 +static int do_imls_nand(void)
  1331 +{
  1332 + nand_info_t *nand;
  1333 + int nand_dev = nand_curr_device;
  1334 + size_t len;
  1335 + loff_t off;
  1336 + u32 buffer[16];
  1337 +
  1338 + if (nand_dev < 0 || nand_dev >= CONFIG_SYS_MAX_NAND_DEVICE) {
  1339 + puts("\nNo NAND devices available\n");
  1340 + return -ENODEV;
  1341 + }
  1342 +
  1343 + printf("\n");
  1344 +
  1345 + for (nand_dev = 0; nand_dev < CONFIG_SYS_MAX_NAND_DEVICE; nand_dev++) {
  1346 + nand = &nand_info[nand_dev];
  1347 + if (!nand->name || !nand->size)
  1348 + continue;
  1349 +
  1350 + for (off = 0; off < nand->size; off += nand->erasesize) {
  1351 + const image_header_t *header;
  1352 + int ret;
  1353 +
  1354 + if (nand_block_isbad(nand, off))
  1355 + continue;
  1356 +
  1357 + len = sizeof(buffer);
  1358 +
  1359 + ret = nand_read(nand, off, &len, (u8 *)buffer);
  1360 + if (ret < 0 && ret != -EUCLEAN) {
  1361 + printf("NAND read error %d at offset %08llX\n",
  1362 + ret, off);
  1363 + continue;
  1364 + }
  1365 +
  1366 + switch (genimg_get_format(buffer)) {
  1367 + case IMAGE_FORMAT_LEGACY:
  1368 + header = (const image_header_t *)buffer;
  1369 +
  1370 + len = image_get_image_size(header);
  1371 + nand_imls_legacyimage(nand, nand_dev, off, len);
  1372 + break;
  1373 +#if defined(CONFIG_FIT)
  1374 + case IMAGE_FORMAT_FIT:
  1375 + len = fit_get_size(buffer);
  1376 + nand_imls_fitimage(nand, nand_dev, off, len);
  1377 + break;
  1378 +#endif
  1379 + }
  1380 + }
  1381 + }
  1382 +
  1383 + return 0;
  1384 +}
  1385 +#endif
  1386 +
  1387 +#if defined(CONFIG_CMD_IMLS) || defined(CONFIG_CMD_IMLS_NAND)
  1388 +static int do_imls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  1389 +{
  1390 + int ret_nor = 0, ret_nand = 0;
  1391 +
  1392 +#if defined(CONFIG_CMD_IMLS)
  1393 + ret_nor = do_imls_nor();
  1394 +#endif
  1395 +
  1396 +#if defined(CONFIG_CMD_IMLS_NAND)
  1397 + ret_nand = do_imls_nand();
  1398 +#endif
  1399 +
  1400 + if (ret_nor)
  1401 + return ret_nor;
  1402 +
  1403 + if (ret_nand)
  1404 + return ret_nand;
  1405 +
1245 1406 return (0);
1246 1407 }
1247 1408  
... ... @@ -1249,8 +1410,8 @@
1249 1410 imls, 1, 1, do_imls,
1250 1411 "list all images found in flash",
1251 1412 "\n"
1252   - " - Prints information about all images found at sector\n"
1253   - " boundaries in flash."
  1413 + " - Prints information about all images found at sector/block\n"
  1414 + " boundaries in nor/nand flash."
1254 1415 );
1255 1416 #endif
1256 1417