blob: cc6e161ba0aebeef3675a0a1cf9f67629695db78 [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>
13
Simon Glass4395f662017-07-29 11:34:54 -060014int blk_common_cmd(int argc, char * const argv[], enum if_type if_type,
15 int *cur_devnump)
16{
17 const char *if_name = blk_get_if_type_name(if_type);
18
19 switch (argc) {
20 case 0:
21 case 1:
22 return CMD_RET_USAGE;
23 case 2:
24 if (strncmp(argv[1], "inf", 3) == 0) {
25 blk_list_devices(if_type);
26 return 0;
27 } else if (strncmp(argv[1], "dev", 3) == 0) {
28 if (blk_print_device_num(if_type, *cur_devnump)) {
29 printf("\nno %s devices available\n", if_name);
30 return CMD_RET_FAILURE;
31 }
32 return 0;
33 } else if (strncmp(argv[1], "part", 4) == 0) {
34 if (blk_list_part(if_type))
Alexandre Besnard45e49682019-12-20 15:25:22 +010035 printf("\nno %s partition table available\n",
36 if_name);
Simon Glass4395f662017-07-29 11:34:54 -060037 return 0;
38 }
39 return CMD_RET_USAGE;
40 case 3:
41 if (strncmp(argv[1], "dev", 3) == 0) {
42 int dev = (int)simple_strtoul(argv[2], NULL, 10);
43
44 if (!blk_show_device(if_type, dev)) {
45 *cur_devnump = dev;
46 printf("... is now current device\n");
47 } else {
48 return CMD_RET_FAILURE;
49 }
50 return 0;
51 } else if (strncmp(argv[1], "part", 4) == 0) {
52 int dev = (int)simple_strtoul(argv[2], NULL, 10);
53
54 if (blk_print_part_devnum(if_type, dev)) {
55 printf("\n%s device %d not available\n",
56 if_name, dev);
57 return CMD_RET_FAILURE;
58 }
59 return 0;
60 }
61 return CMD_RET_USAGE;
62
63 default: /* at least 4 args */
64 if (strcmp(argv[1], "read") == 0) {
65 ulong addr = simple_strtoul(argv[2], NULL, 16);
66 lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
67 ulong cnt = simple_strtoul(argv[4], NULL, 16);
68 ulong n;
69
Bin Menge2888a72017-09-12 19:00:36 -070070 printf("\n%s read: device %d block # "LBAFU", count %lu ... ",
71 if_name, *cur_devnump, blk, cnt);
Simon Glass4395f662017-07-29 11:34:54 -060072
73 n = blk_read_devnum(if_type, *cur_devnump, blk, cnt,
74 (ulong *)addr);
75
76 printf("%ld blocks read: %s\n", n,
77 n == cnt ? "OK" : "ERROR");
78 return n == cnt ? 0 : 1;
79 } else if (strcmp(argv[1], "write") == 0) {
80 ulong addr = simple_strtoul(argv[2], NULL, 16);
81 lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
82 ulong cnt = simple_strtoul(argv[4], NULL, 16);
83 ulong n;
84
Bin Menge2888a72017-09-12 19:00:36 -070085 printf("\n%s write: device %d block # "LBAFU", count %lu ... ",
86 if_name, *cur_devnump, blk, cnt);
Simon Glass4395f662017-07-29 11:34:54 -060087
88 n = blk_write_devnum(if_type, *cur_devnump, blk, cnt,
89 (ulong *)addr);
90
91 printf("%ld blocks written: %s\n", n,
92 n == cnt ? "OK" : "ERROR");
93 return n == cnt ? 0 : 1;
94 } else {
95 return CMD_RET_USAGE;
96 }
Simon Glass4395f662017-07-29 11:34:54 -060097 }
98}