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.
16 int blk_common_cmd(int argc, char *const argv[], enum uclass_id uclass_id,
19 const char *if_name = blk_get_uclass_name(uclass_id);
26 if (strncmp(argv[1], "inf", 3) == 0) {
27 blk_list_devices(uclass_id);
29 } else if (strncmp(argv[1], "dev", 3) == 0) {
30 if (blk_print_device_num(uclass_id, *cur_devnump)) {
31 printf("\nno %s devices available\n", if_name);
32 return CMD_RET_FAILURE;
35 } else if (strncmp(argv[1], "part", 4) == 0) {
36 if (blk_list_part(uclass_id))
37 printf("\nno %s partition table available\n",
43 if (strncmp(argv[1], "dev", 3) == 0) {
44 int dev = (int)dectoul(argv[2], NULL);
46 if (!blk_show_device(uclass_id, dev)) {
48 printf("... is now current device\n");
50 return CMD_RET_FAILURE;
53 } else if (strncmp(argv[1], "part", 4) == 0) {
54 int dev = (int)dectoul(argv[2], NULL);
56 if (blk_print_part_devnum(uclass_id, dev)) {
57 printf("\n%s device %d not available\n",
59 return CMD_RET_FAILURE;
65 default: /* at least 4 args */
66 if (strcmp(argv[1], "read") == 0) {
67 phys_addr_t paddr = hextoul(argv[2], NULL);
68 lbaint_t blk = hextoul(argv[3], NULL);
69 ulong cnt = hextoul(argv[4], NULL);
73 printf("\n%s read: device %d block # "LBAFU", count %lu ... ",
74 if_name, *cur_devnump, blk, cnt);
76 vaddr = map_sysmem(paddr, 512 * cnt);
77 n = blk_read_devnum(uclass_id, *cur_devnump, blk, cnt,
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) {
85 phys_addr_t paddr = hextoul(argv[2], NULL);
86 lbaint_t blk = hextoul(argv[3], NULL);
87 ulong cnt = hextoul(argv[4], NULL);
91 printf("\n%s write: device %d block # "LBAFU", count %lu ... ",
92 if_name, *cur_devnump, blk, cnt);
94 vaddr = map_sysmem(paddr, 512 * cnt);
95 n = blk_write_devnum(uclass_id, *cur_devnump, blk, cnt,
99 printf("%ld blocks written: %s\n", n,
100 n == cnt ? "OK" : "ERROR");
101 return n == cnt ? 0 : 1;
103 return CMD_RET_USAGE;