1 /*
2  * Copyright (c) 2019 Intel Corporation
3  * Copyright (c) 2023 Sensorfy B.V.
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/shell/shell.h>
8 #include <zephyr/init.h>
9 #include <string.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <ctype.h>
13 #include <zephyr/storage/flash_map.h>
14 #include <zephyr/logging/log.h>
15 #include <zephyr/device.h>
16 
17 #define LOG_LEVEL CONFIG_LOG_DEFAULT_LEVEL
18 
19 LOG_MODULE_REGISTER(flash_map_shell);
20 
21 extern const struct flash_area *flash_map;
22 
fa_cb(const struct flash_area * fa,void * user_data)23 static void fa_cb(const struct flash_area *fa, void *user_data)
24 {
25 	struct shell *sh = user_data;
26 #if CONFIG_FLASH_MAP_LABELS
27 	const char *fa_label = flash_area_label(fa);
28 
29 	if (fa_label == NULL) {
30 		fa_label = "-";
31 	}
32 	shell_print(sh, "%2d   0x%0*" PRIxPTR "   %-26s  %-24.24s  0x%-10x 0x%-12x", (int)fa->fa_id,
33 		    sizeof(uintptr_t) * 2, (uintptr_t)fa->fa_dev, fa->fa_dev->name, fa_label,
34 		    (uint32_t)fa->fa_off, fa->fa_size);
35 #else
36 	shell_print(sh, "%2d   0x%0*" PRIxPTR "   %-26s  0x%-10x 0x%-12x", (int)fa->fa_id,
37 		    sizeof(uintptr_t) * 2, (uintptr_t)fa->fa_dev, fa->fa_dev->name,
38 		    (uint32_t)fa->fa_off, fa->fa_size);
39 #endif
40 }
41 
cmd_flash_map_list(const struct shell * sh,size_t argc,char ** argv)42 static int cmd_flash_map_list(const struct shell *sh, size_t argc, char **argv)
43 {
44 #if CONFIG_FLASH_MAP_LABELS
45 	shell_print(sh, "ID | Device     | Device Name               "
46 			"| Label                   | Offset     | Size");
47 	shell_print(sh, "--------------------------------------------"
48 			"-----------------------------------------------");
49 #else
50 	shell_print(sh, "ID | Device     | Device Name               "
51 			"| Offset     | Size");
52 	shell_print(sh, "-----------------------------------------"
53 			"------------------------------");
54 #endif
55 	flash_area_foreach(fa_cb, (struct shell *)sh);
56 	return 0;
57 }
58 
59 SHELL_STATIC_SUBCMD_SET_CREATE(sub_flash_map,
60 	/* Alphabetically sorted. */
61 	SHELL_CMD(list, NULL, "List flash areas", cmd_flash_map_list),
62 	SHELL_SUBCMD_SET_END /* Array terminated. */
63 );
64 
65 SHELL_CMD_REGISTER(flash_map, &sub_flash_map, "Flash map commands", NULL);
66