Simon Glass | bf2e795 | 2017-07-29 11:34:54 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Handling of common block commands |
| 3 | * |
| 4 | * Copyright (c) 2017 Google, Inc |
| 5 | * |
| 6 | * (C) Copyright 2000-2011 |
| 7 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 8 | * |
| 9 | * SPDX-License-Identifier: GPL-2.0+ |
| 10 | */ |
| 11 | |
| 12 | #include <common.h> |
| 13 | #include <blk.h> |
| 14 | |
| 15 | #ifdef HAVE_BLOCK_DEVICE |
| 16 | int blk_common_cmd(int argc, char * const argv[], enum if_type if_type, |
| 17 | int *cur_devnump) |
| 18 | { |
| 19 | const char *if_name = blk_get_if_type_name(if_type); |
| 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) { |
| 27 | blk_list_devices(if_type); |
| 28 | return 0; |
| 29 | } else if (strncmp(argv[1], "dev", 3) == 0) { |
| 30 | if (blk_print_device_num(if_type, *cur_devnump)) { |
| 31 | printf("\nno %s devices available\n", if_name); |
| 32 | return CMD_RET_FAILURE; |
| 33 | } |
| 34 | return 0; |
| 35 | } else if (strncmp(argv[1], "part", 4) == 0) { |
| 36 | if (blk_list_part(if_type)) |
| 37 | printf("\nno %s devices available\n", if_name); |
| 38 | return 0; |
| 39 | } |
| 40 | return CMD_RET_USAGE; |
| 41 | case 3: |
| 42 | if (strncmp(argv[1], "dev", 3) == 0) { |
| 43 | int dev = (int)simple_strtoul(argv[2], NULL, 10); |
| 44 | |
| 45 | if (!blk_show_device(if_type, dev)) { |
| 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) { |
| 53 | int dev = (int)simple_strtoul(argv[2], NULL, 10); |
| 54 | |
| 55 | if (blk_print_part_devnum(if_type, dev)) { |
| 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) { |
| 66 | ulong addr = simple_strtoul(argv[2], NULL, 16); |
| 67 | lbaint_t blk = simple_strtoul(argv[3], NULL, 16); |
| 68 | ulong cnt = simple_strtoul(argv[4], NULL, 16); |
| 69 | ulong n; |
| 70 | |
| 71 | printf("\n%s read: device %d block # %lld, count %ld ... ", |
| 72 | if_name, *cur_devnump, (unsigned long long)blk, |
| 73 | cnt); |
| 74 | |
| 75 | n = blk_read_devnum(if_type, *cur_devnump, blk, cnt, |
| 76 | (ulong *)addr); |
| 77 | |
| 78 | printf("%ld blocks read: %s\n", n, |
| 79 | n == cnt ? "OK" : "ERROR"); |
| 80 | return n == cnt ? 0 : 1; |
| 81 | } else if (strcmp(argv[1], "write") == 0) { |
| 82 | ulong addr = simple_strtoul(argv[2], NULL, 16); |
| 83 | lbaint_t blk = simple_strtoul(argv[3], NULL, 16); |
| 84 | ulong cnt = simple_strtoul(argv[4], NULL, 16); |
| 85 | ulong n; |
| 86 | |
| 87 | printf("\n%s write: device %d block # %lld, count %ld ... ", |
| 88 | if_name, *cur_devnump, (unsigned long long)blk, |
| 89 | cnt); |
| 90 | |
| 91 | n = blk_write_devnum(if_type, *cur_devnump, blk, cnt, |
| 92 | (ulong *)addr); |
| 93 | |
| 94 | printf("%ld blocks written: %s\n", n, |
| 95 | n == cnt ? "OK" : "ERROR"); |
| 96 | return n == cnt ? 0 : 1; |
| 97 | } else { |
| 98 | return CMD_RET_USAGE; |
| 99 | } |
| 100 | |
| 101 | return 0; |
| 102 | } |
| 103 | } |
| 104 | #endif |