1 // SPDX-License-Identifier: GPL-2.0+
3 * Handling of common block commands
5 * Copyright (c) 2017 Google, Inc
7 * (C) Copyright 2000-2011
8 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
15 int blk_common_cmd(int argc, char *const argv[], enum uclass_id uclass_id,
18 const char *if_name = blk_get_uclass_name(uclass_id);
25 if (strncmp(argv[1], "inf", 3) == 0) {
26 blk_list_devices(uclass_id);
28 } else if (strncmp(argv[1], "dev", 3) == 0) {
29 if (blk_print_device_num(uclass_id, *cur_devnump)) {
30 printf("\nno %s devices available\n", if_name);
31 return CMD_RET_FAILURE;
34 } else if (strncmp(argv[1], "part", 4) == 0) {
35 if (blk_list_part(uclass_id))
36 printf("\nno %s partition table available\n",
42 if (strncmp(argv[1], "dev", 3) == 0) {
43 int dev = (int)dectoul(argv[2], NULL);
45 if (!blk_show_device(uclass_id, dev)) {
47 printf("... is now current device\n");
49 return CMD_RET_FAILURE;
52 } else if (strncmp(argv[1], "part", 4) == 0) {
53 int dev = (int)dectoul(argv[2], NULL);
55 if (blk_print_part_devnum(uclass_id, dev)) {
56 printf("\n%s device %d not available\n",
58 return CMD_RET_FAILURE;
64 default: /* at least 4 args */
65 if (strcmp(argv[1], "read") == 0) {
66 ulong addr = hextoul(argv[2], NULL);
67 lbaint_t blk = hextoul(argv[3], NULL);
68 ulong cnt = hextoul(argv[4], NULL);
71 printf("\n%s read: device %d block # "LBAFU", count %lu ... ",
72 if_name, *cur_devnump, blk, cnt);
74 n = blk_read_devnum(uclass_id, *cur_devnump, blk, cnt,
77 printf("%ld blocks read: %s\n", n,
78 n == cnt ? "OK" : "ERROR");
79 return n == cnt ? 0 : 1;
80 } else if (strcmp(argv[1], "write") == 0) {
81 ulong addr = hextoul(argv[2], NULL);
82 lbaint_t blk = hextoul(argv[3], NULL);
83 ulong cnt = hextoul(argv[4], NULL);
86 printf("\n%s write: device %d block # "LBAFU", count %lu ... ",
87 if_name, *cur_devnump, blk, cnt);
89 n = blk_write_devnum(uclass_id, *cur_devnump, blk, cnt,
92 printf("%ld blocks written: %s\n", n,
93 n == cnt ? "OK" : "ERROR");
94 return n == cnt ? 0 : 1;