Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 4395f66 | 2017-07-29 11:34:54 -0600 | [diff] [blame] | 2 | /* |
| 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 Glass | 4395f66 | 2017-07-29 11:34:54 -0600 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <common.h> |
| 12 | #include <blk.h> |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 13 | #include <command.h> |
Simon Glass | 4395f66 | 2017-07-29 11:34:54 -0600 | [diff] [blame] | 14 | |
Simon Glass | 8149b15 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 15 | int blk_common_cmd(int argc, char *const argv[], enum uclass_id uclass_id, |
Simon Glass | 4395f66 | 2017-07-29 11:34:54 -0600 | [diff] [blame] | 16 | int *cur_devnump) |
| 17 | { |
Simon Glass | 8149b15 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 18 | const char *if_name = blk_get_uclass_name(uclass_id); |
Simon Glass | 4395f66 | 2017-07-29 11:34:54 -0600 | [diff] [blame] | 19 | |
| 20 | switch (argc) { |
| 21 | case 0: |
| 22 | case 1: |
| 23 | return CMD_RET_USAGE; |
| 24 | case 2: |
| 25 | if (strncmp(argv[1], "inf", 3) == 0) { |
Simon Glass | 8149b15 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 26 | blk_list_devices(uclass_id); |
Simon Glass | 4395f66 | 2017-07-29 11:34:54 -0600 | [diff] [blame] | 27 | return 0; |
| 28 | } else if (strncmp(argv[1], "dev", 3) == 0) { |
Simon Glass | 8149b15 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 29 | if (blk_print_device_num(uclass_id, *cur_devnump)) { |
Simon Glass | 4395f66 | 2017-07-29 11:34:54 -0600 | [diff] [blame] | 30 | printf("\nno %s devices available\n", if_name); |
| 31 | return CMD_RET_FAILURE; |
| 32 | } |
| 33 | return 0; |
| 34 | } else if (strncmp(argv[1], "part", 4) == 0) { |
Simon Glass | 8149b15 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 35 | if (blk_list_part(uclass_id)) |
Alexandre Besnard | 45e4968 | 2019-12-20 15:25:22 +0100 | [diff] [blame] | 36 | printf("\nno %s partition table available\n", |
| 37 | if_name); |
Simon Glass | 4395f66 | 2017-07-29 11:34:54 -0600 | [diff] [blame] | 38 | return 0; |
| 39 | } |
| 40 | return CMD_RET_USAGE; |
| 41 | case 3: |
| 42 | if (strncmp(argv[1], "dev", 3) == 0) { |
Simon Glass | 0b1284e | 2021-07-24 09:03:30 -0600 | [diff] [blame] | 43 | int dev = (int)dectoul(argv[2], NULL); |
Simon Glass | 4395f66 | 2017-07-29 11:34:54 -0600 | [diff] [blame] | 44 | |
Simon Glass | 8149b15 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 45 | if (!blk_show_device(uclass_id, dev)) { |
Simon Glass | 4395f66 | 2017-07-29 11:34:54 -0600 | [diff] [blame] | 46 | *cur_devnump = dev; |
| 47 | printf("... is now current device\n"); |
| 48 | } else { |
| 49 | return CMD_RET_FAILURE; |
| 50 | } |
| 51 | return 0; |
| 52 | } else if (strncmp(argv[1], "part", 4) == 0) { |
Simon Glass | 0b1284e | 2021-07-24 09:03:30 -0600 | [diff] [blame] | 53 | int dev = (int)dectoul(argv[2], NULL); |
Simon Glass | 4395f66 | 2017-07-29 11:34:54 -0600 | [diff] [blame] | 54 | |
Simon Glass | 8149b15 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 55 | if (blk_print_part_devnum(uclass_id, dev)) { |
Simon Glass | 4395f66 | 2017-07-29 11:34:54 -0600 | [diff] [blame] | 56 | printf("\n%s device %d not available\n", |
| 57 | if_name, dev); |
| 58 | return CMD_RET_FAILURE; |
| 59 | } |
| 60 | return 0; |
| 61 | } |
| 62 | return CMD_RET_USAGE; |
| 63 | |
| 64 | default: /* at least 4 args */ |
| 65 | if (strcmp(argv[1], "read") == 0) { |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 66 | ulong addr = hextoul(argv[2], NULL); |
| 67 | lbaint_t blk = hextoul(argv[3], NULL); |
| 68 | ulong cnt = hextoul(argv[4], NULL); |
Simon Glass | 4395f66 | 2017-07-29 11:34:54 -0600 | [diff] [blame] | 69 | ulong n; |
| 70 | |
Bin Meng | e2888a7 | 2017-09-12 19:00:36 -0700 | [diff] [blame] | 71 | printf("\n%s read: device %d block # "LBAFU", count %lu ... ", |
| 72 | if_name, *cur_devnump, blk, cnt); |
Simon Glass | 4395f66 | 2017-07-29 11:34:54 -0600 | [diff] [blame] | 73 | |
Simon Glass | 8149b15 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 74 | n = blk_read_devnum(uclass_id, *cur_devnump, blk, cnt, |
Simon Glass | 4395f66 | 2017-07-29 11:34:54 -0600 | [diff] [blame] | 75 | (ulong *)addr); |
| 76 | |
| 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) { |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 81 | ulong addr = hextoul(argv[2], NULL); |
| 82 | lbaint_t blk = hextoul(argv[3], NULL); |
| 83 | ulong cnt = hextoul(argv[4], NULL); |
Simon Glass | 4395f66 | 2017-07-29 11:34:54 -0600 | [diff] [blame] | 84 | ulong n; |
| 85 | |
Bin Meng | e2888a7 | 2017-09-12 19:00:36 -0700 | [diff] [blame] | 86 | printf("\n%s write: device %d block # "LBAFU", count %lu ... ", |
| 87 | if_name, *cur_devnump, blk, cnt); |
Simon Glass | 4395f66 | 2017-07-29 11:34:54 -0600 | [diff] [blame] | 88 | |
Simon Glass | 8149b15 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 89 | n = blk_write_devnum(uclass_id, *cur_devnump, blk, cnt, |
Simon Glass | 4395f66 | 2017-07-29 11:34:54 -0600 | [diff] [blame] | 90 | (ulong *)addr); |
| 91 | |
| 92 | printf("%ld blocks written: %s\n", n, |
| 93 | n == cnt ? "OK" : "ERROR"); |
| 94 | return n == cnt ? 0 : 1; |
| 95 | } else { |
| 96 | return CMD_RET_USAGE; |
| 97 | } |
Simon Glass | 4395f66 | 2017-07-29 11:34:54 -0600 | [diff] [blame] | 98 | } |
| 99 | } |