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