blob: 4c05a4e0610e0bf5c32c1105317bae274b3a7d96 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass4395f662017-07-29 11:34:54 -06002/*
3 * Handling of common block commands
4 *
5 * Copyright (c) 2017 Google, Inc
6 *
7 * (C) Copyright 2000-2011
8 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
Simon Glass4395f662017-07-29 11:34:54 -06009 */
10
Simon Glass4395f662017-07-29 11:34:54 -060011#include <blk.h>
Simon Glass09140112020-05-10 11:40:03 -060012#include <command.h>
Tobias Waldekranz3d2fc792023-02-16 16:33:48 +010013#include <mapmem.h>
Tom Rini03de3052024-05-20 13:35:03 -060014#include <vsprintf.h>
Simon Glass4395f662017-07-29 11:34:54 -060015
Simon Glass8149b152022-09-17 09:00:09 -060016int blk_common_cmd(int argc, char *const argv[], enum uclass_id uclass_id,
Simon Glass4395f662017-07-29 11:34:54 -060017 int *cur_devnump)
18{
Simon Glass8149b152022-09-17 09:00:09 -060019 const char *if_name = blk_get_uclass_name(uclass_id);
Simon Glass4395f662017-07-29 11:34:54 -060020
21 switch (argc) {
22 case 0:
23 case 1:
24 return CMD_RET_USAGE;
25 case 2:
26 if (strncmp(argv[1], "inf", 3) == 0) {
Simon Glass8149b152022-09-17 09:00:09 -060027 blk_list_devices(uclass_id);
Bin Meng8ccc9482023-09-26 16:43:40 +080028 return CMD_RET_SUCCESS;
Simon Glass4395f662017-07-29 11:34:54 -060029 } else if (strncmp(argv[1], "dev", 3) == 0) {
Simon Glass8149b152022-09-17 09:00:09 -060030 if (blk_print_device_num(uclass_id, *cur_devnump)) {
Simon Glass4395f662017-07-29 11:34:54 -060031 printf("\nno %s devices available\n", if_name);
32 return CMD_RET_FAILURE;
33 }
Bin Meng8ccc9482023-09-26 16:43:40 +080034 return CMD_RET_SUCCESS;
Simon Glass4395f662017-07-29 11:34:54 -060035 } else if (strncmp(argv[1], "part", 4) == 0) {
Simon Glass8149b152022-09-17 09:00:09 -060036 if (blk_list_part(uclass_id))
Alexandre Besnard45e49682019-12-20 15:25:22 +010037 printf("\nno %s partition table available\n",
38 if_name);
Bin Meng8ccc9482023-09-26 16:43:40 +080039 return CMD_RET_SUCCESS;
Simon Glass4395f662017-07-29 11:34:54 -060040 }
41 return CMD_RET_USAGE;
42 case 3:
43 if (strncmp(argv[1], "dev", 3) == 0) {
Simon Glass0b1284e2021-07-24 09:03:30 -060044 int dev = (int)dectoul(argv[2], NULL);
Simon Glass4395f662017-07-29 11:34:54 -060045
Simon Glass8149b152022-09-17 09:00:09 -060046 if (!blk_show_device(uclass_id, dev)) {
Simon Glass4395f662017-07-29 11:34:54 -060047 *cur_devnump = dev;
48 printf("... is now current device\n");
49 } else {
50 return CMD_RET_FAILURE;
51 }
Bin Meng8ccc9482023-09-26 16:43:40 +080052 return CMD_RET_SUCCESS;
Simon Glass4395f662017-07-29 11:34:54 -060053 } else if (strncmp(argv[1], "part", 4) == 0) {
Simon Glass0b1284e2021-07-24 09:03:30 -060054 int dev = (int)dectoul(argv[2], NULL);
Simon Glass4395f662017-07-29 11:34:54 -060055
Simon Glass8149b152022-09-17 09:00:09 -060056 if (blk_print_part_devnum(uclass_id, dev)) {
Simon Glass4395f662017-07-29 11:34:54 -060057 printf("\n%s device %d not available\n",
58 if_name, dev);
59 return CMD_RET_FAILURE;
60 }
Bin Meng8ccc9482023-09-26 16:43:40 +080061 return CMD_RET_SUCCESS;
Simon Glass4395f662017-07-29 11:34:54 -060062 }
63 return CMD_RET_USAGE;
64
65 default: /* at least 4 args */
66 if (strcmp(argv[1], "read") == 0) {
Tobias Waldekranz3d2fc792023-02-16 16:33:48 +010067 phys_addr_t paddr = hextoul(argv[2], NULL);
Simon Glass7e5f4602021-07-24 09:03:29 -060068 lbaint_t blk = hextoul(argv[3], NULL);
69 ulong cnt = hextoul(argv[4], NULL);
Bin Meng4e345652023-09-26 16:43:42 +080070 struct blk_desc *desc;
Tobias Waldekranz3d2fc792023-02-16 16:33:48 +010071 void *vaddr;
Simon Glass4395f662017-07-29 11:34:54 -060072 ulong n;
Bin Meng4e345652023-09-26 16:43:42 +080073 int ret;
Simon Glass4395f662017-07-29 11:34:54 -060074
Bin Menge2888a72017-09-12 19:00:36 -070075 printf("\n%s read: device %d block # "LBAFU", count %lu ... ",
76 if_name, *cur_devnump, blk, cnt);
Simon Glass4395f662017-07-29 11:34:54 -060077
Bin Meng4e345652023-09-26 16:43:42 +080078 ret = blk_get_desc(uclass_id, *cur_devnump, &desc);
79 if (ret)
80 return CMD_RET_FAILURE;
81 vaddr = map_sysmem(paddr, desc->blksz * cnt);
82 n = blk_dread(desc, blk, cnt, vaddr);
Tobias Waldekranz3d2fc792023-02-16 16:33:48 +010083 unmap_sysmem(vaddr);
Simon Glass4395f662017-07-29 11:34:54 -060084
85 printf("%ld blocks read: %s\n", n,
86 n == cnt ? "OK" : "ERROR");
Bin Meng8ccc9482023-09-26 16:43:40 +080087 return n == cnt ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
Simon Glass4395f662017-07-29 11:34:54 -060088 } else if (strcmp(argv[1], "write") == 0) {
Tobias Waldekranz3d2fc792023-02-16 16:33:48 +010089 phys_addr_t paddr = hextoul(argv[2], NULL);
Simon Glass7e5f4602021-07-24 09:03:29 -060090 lbaint_t blk = hextoul(argv[3], NULL);
91 ulong cnt = hextoul(argv[4], NULL);
Bin Meng4e345652023-09-26 16:43:42 +080092 struct blk_desc *desc;
Tobias Waldekranz3d2fc792023-02-16 16:33:48 +010093 void *vaddr;
Simon Glass4395f662017-07-29 11:34:54 -060094 ulong n;
Bin Meng4e345652023-09-26 16:43:42 +080095 int ret;
Simon Glass4395f662017-07-29 11:34:54 -060096
Bin Menge2888a72017-09-12 19:00:36 -070097 printf("\n%s write: device %d block # "LBAFU", count %lu ... ",
98 if_name, *cur_devnump, blk, cnt);
Simon Glass4395f662017-07-29 11:34:54 -060099
Bin Meng4e345652023-09-26 16:43:42 +0800100 ret = blk_get_desc(uclass_id, *cur_devnump, &desc);
101 if (ret)
102 return CMD_RET_FAILURE;
103 vaddr = map_sysmem(paddr, desc->blksz * cnt);
104 n = blk_dwrite(desc, blk, cnt, vaddr);
Tobias Waldekranz3d2fc792023-02-16 16:33:48 +0100105 unmap_sysmem(vaddr);
Simon Glass4395f662017-07-29 11:34:54 -0600106
107 printf("%ld blocks written: %s\n", n,
108 n == cnt ? "OK" : "ERROR");
Bin Meng8ccc9482023-09-26 16:43:40 +0800109 return n == cnt ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
Simon Glass4395f662017-07-29 11:34:54 -0600110 } else {
111 return CMD_RET_USAGE;
112 }
Simon Glass4395f662017-07-29 11:34:54 -0600113 }
114}