1 /*
2 * Copyright (C) 2022 Beken Corporation
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16
17 #include <driver/flash.h>
18 #include <driver/flash_partition.h>
19 #include "cli.h"
20 #include "flash_driver.h"
21
cli_flash_help(void)22 static void cli_flash_help(void)
23 {
24 CLI_LOGI("flash driver init\n");
25 CLI_LOGI("flash_driver deinit\n");
26 CLI_LOGI("flash {erase|write|read} [start_addr] [len]\n");
27 CLI_LOGI("flash_partition show\n");
28 }
29
cli_flash_cmd(char * pcWriteBuffer,int xWriteBufferLen,int argc,char ** argv)30 static void cli_flash_cmd(char *pcWriteBuffer, int xWriteBufferLen, int argc, char **argv)
31 {
32 if (argc < 2) {
33 cli_flash_help();
34 return;
35 }
36
37 uint32_t start_addr = os_strtoul(argv[2], NULL, 16);
38 uint32_t len = os_strtoul(argv[3], NULL, 10);
39
40 if (os_strcmp(argv[1], "erase") == 0) {
41 bk_flash_set_protect_type(FLASH_PROTECT_NONE);
42 for (uint32_t addr = start_addr; addr < (start_addr + len); addr += FLASH_SECTOR_SIZE) {
43 bk_flash_erase_sector(addr);
44 }
45 bk_flash_set_protect_type(FLASH_UNPROTECT_LAST_BLOCK);
46 } else if (os_strcmp(argv[1], "read") == 0) {
47 uint8_t buf[FLASH_PAGE_SIZE] = {0};
48 for (uint32_t addr = start_addr; addr < (start_addr + len); addr += FLASH_PAGE_SIZE) {
49 os_memset(buf, 0, FLASH_PAGE_SIZE);
50 bk_flash_read_bytes(addr, buf, FLASH_PAGE_SIZE);
51 CLI_LOGI("flash read addr:%x\r\n", addr);
52
53 CLI_LOGI("dump read flash data:\r\n");
54 for (uint32_t i = 0; i < 16; i++) {
55 for (uint32_t j = 0; j < 16; j++) {
56 os_printf("%02x ", buf[i * 16 + j]);
57 }
58 os_printf("\r\n");
59 }
60 }
61 } else if (os_strcmp(argv[1], "write") == 0) {
62 uint8_t buf[FLASH_PAGE_SIZE] = {0};
63 for (uint32_t i = 0; i < FLASH_PAGE_SIZE; i++) {
64 buf[i] = i;
65 }
66 bk_flash_set_protect_type(FLASH_PROTECT_NONE);
67 for (uint32_t addr = start_addr; addr < (start_addr + len); addr += FLASH_PAGE_SIZE) {
68 bk_flash_write_bytes(addr, buf, FLASH_PAGE_SIZE);
69 }
70 bk_flash_set_protect_type(FLASH_UNPROTECT_LAST_BLOCK);
71 } else if (os_strcmp(argv[1], "get_id") == 0) {
72 uint32_t flash_id = bk_flash_get_id();
73 CLI_LOGI("flash_id:%x\r\n", flash_id);
74 } else {
75 cli_flash_help();
76 }
77 }
78
cli_flash_partition_cmd(char * pcWriteBuffer,int xWriteBufferLen,int argc,char ** argv)79 static void cli_flash_partition_cmd(char *pcWriteBuffer, int xWriteBufferLen, int argc, char **argv)
80 {
81 bk_logic_partition_t *partition;
82
83 if (os_strcmp(argv[1], "show") == 0) {
84 for (bk_partition_t par= BK_PARTITION_BOOTLOADER; par <= BK_PARTITION_MAX; par++) {
85 partition = bk_flash_partition_get_info(par);
86 if (partition == NULL)
87 continue;
88
89 CLI_LOGI("%4d | %11s | Dev:%d | 0x%08lx | 0x%08lx |\r\n", par,
90 partition->partition_description, partition->partition_owner,
91 partition->partition_start_addr, partition->partition_length);
92 }
93 } else {
94 cli_flash_help();
95 }
96 }
97
98 #define FLASH_CMD_CNT (sizeof(s_flash_commands) / sizeof(struct cli_command))
99 static const struct cli_command s_flash_commands[] = {
100 {"flash", "flash {erase|read|write} [start_addr] [len]", cli_flash_cmd},
101 {"flash_partition", "flash_partition {show}", cli_flash_partition_cmd},
102 };
103
cli_flash_init(void)104 int cli_flash_init(void)
105 {
106 BK_LOG_ON_ERR(bk_flash_driver_init());
107 return cli_register_commands(s_flash_commands, FLASH_CMD_CNT);
108 }
109
110