blob: 9f9d4327a99683679deaf5855a41f0c6c880c81f [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
11#include <common.h>
12#include <blk.h>
Simon Glass09140112020-05-10 11:40:03 -060013#include <command.h>
Tobias Waldekranz3d2fc792023-02-16 16:33:48 +010014#include <mapmem.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);
Simon Glass4395f662017-07-29 11:34:54 -060028 return 0;
29 } 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 }
34 return 0;
35 } 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);
Simon Glass4395f662017-07-29 11:34:54 -060039 return 0;
40 }
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 }
52 return 0;
53 } 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 }
61 return 0;
62 }
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);
Tobias Waldekranz3d2fc792023-02-16 16:33:48 +010070 void *vaddr;
Simon Glass4395f662017-07-29 11:34:54 -060071 ulong n;
72
Bin Menge2888a72017-09-12 19:00:36 -070073 printf("\n%s read: device %d block # "LBAFU", count %lu ... ",
74 if_name, *cur_devnump, blk, cnt);
Simon Glass4395f662017-07-29 11:34:54 -060075
Tobias Waldekranz3d2fc792023-02-16 16:33:48 +010076 vaddr = map_sysmem(paddr, 512 * cnt);
Simon Glass8149b152022-09-17 09:00:09 -060077 n = blk_read_devnum(uclass_id, *cur_devnump, blk, cnt,
Tobias Waldekranz3d2fc792023-02-16 16:33:48 +010078 vaddr);
79 unmap_sysmem(vaddr);
Simon Glass4395f662017-07-29 11:34:54 -060080
81 printf("%ld blocks read: %s\n", n,
82 n == cnt ? "OK" : "ERROR");
83 return n == cnt ? 0 : 1;
84 } else if (strcmp(argv[1], "write") == 0) {
Tobias Waldekranz3d2fc792023-02-16 16:33:48 +010085 phys_addr_t paddr = hextoul(argv[2], NULL);
Simon Glass7e5f4602021-07-24 09:03:29 -060086 lbaint_t blk = hextoul(argv[3], NULL);
87 ulong cnt = hextoul(argv[4], NULL);
Tobias Waldekranz3d2fc792023-02-16 16:33:48 +010088 void *vaddr;
Simon Glass4395f662017-07-29 11:34:54 -060089 ulong n;
90
Bin Menge2888a72017-09-12 19:00:36 -070091 printf("\n%s write: device %d block # "LBAFU", count %lu ... ",
92 if_name, *cur_devnump, blk, cnt);
Simon Glass4395f662017-07-29 11:34:54 -060093
Tobias Waldekranz3d2fc792023-02-16 16:33:48 +010094 vaddr = map_sysmem(paddr, 512 * cnt);
Simon Glass8149b152022-09-17 09:00:09 -060095 n = blk_write_devnum(uclass_id, *cur_devnump, blk, cnt,
Tobias Waldekranz3d2fc792023-02-16 16:33:48 +010096 vaddr);
97 unmap_sysmem(vaddr);
Simon Glass4395f662017-07-29 11:34:54 -060098
99 printf("%ld blocks written: %s\n", n,
100 n == cnt ? "OK" : "ERROR");
101 return n == cnt ? 0 : 1;
102 } else {
103 return CMD_RET_USAGE;
104 }
Simon Glass4395f662017-07-29 11:34:54 -0600105 }
106}