blob: ad9b16dc098d3a99cce8d3e9c655ab21329e364b [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassbf2e7952017-07-29 11:34:54 -06002/*
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 Glassbf2e7952017-07-29 11:34:54 -06009 */
10
11#include <common.h>
12#include <blk.h>
Simon Glassed38aef2020-05-10 11:40:03 -060013#include <command.h>
Tobias Waldekranz0086a172023-02-16 16:33:48 +010014#include <mapmem.h>
Simon Glassbf2e7952017-07-29 11:34:54 -060015
Simon Glassfada3f92022-09-17 09:00:09 -060016int blk_common_cmd(int argc, char *const argv[], enum uclass_id uclass_id,
Simon Glassbf2e7952017-07-29 11:34:54 -060017 int *cur_devnump)
18{
Simon Glassfada3f92022-09-17 09:00:09 -060019 const char *if_name = blk_get_uclass_name(uclass_id);
Simon Glassbf2e7952017-07-29 11:34:54 -060020
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 Glassfada3f92022-09-17 09:00:09 -060027 blk_list_devices(uclass_id);
Bin Meng76f72532023-09-26 16:43:40 +080028 return CMD_RET_SUCCESS;
Simon Glassbf2e7952017-07-29 11:34:54 -060029 } else if (strncmp(argv[1], "dev", 3) == 0) {
Simon Glassfada3f92022-09-17 09:00:09 -060030 if (blk_print_device_num(uclass_id, *cur_devnump)) {
Simon Glassbf2e7952017-07-29 11:34:54 -060031 printf("\nno %s devices available\n", if_name);
32 return CMD_RET_FAILURE;
33 }
Bin Meng76f72532023-09-26 16:43:40 +080034 return CMD_RET_SUCCESS;
Simon Glassbf2e7952017-07-29 11:34:54 -060035 } else if (strncmp(argv[1], "part", 4) == 0) {
Simon Glassfada3f92022-09-17 09:00:09 -060036 if (blk_list_part(uclass_id))
Alexandre Besnardd2dc8c52019-12-20 15:25:22 +010037 printf("\nno %s partition table available\n",
38 if_name);
Bin Meng76f72532023-09-26 16:43:40 +080039 return CMD_RET_SUCCESS;
Simon Glassbf2e7952017-07-29 11:34:54 -060040 }
41 return CMD_RET_USAGE;
42 case 3:
43 if (strncmp(argv[1], "dev", 3) == 0) {
Simon Glassff9b9032021-07-24 09:03:30 -060044 int dev = (int)dectoul(argv[2], NULL);
Simon Glassbf2e7952017-07-29 11:34:54 -060045
Simon Glassfada3f92022-09-17 09:00:09 -060046 if (!blk_show_device(uclass_id, dev)) {
Simon Glassbf2e7952017-07-29 11:34:54 -060047 *cur_devnump = dev;
48 printf("... is now current device\n");
49 } else {
50 return CMD_RET_FAILURE;
51 }
Bin Meng76f72532023-09-26 16:43:40 +080052 return CMD_RET_SUCCESS;
Simon Glassbf2e7952017-07-29 11:34:54 -060053 } else if (strncmp(argv[1], "part", 4) == 0) {
Simon Glassff9b9032021-07-24 09:03:30 -060054 int dev = (int)dectoul(argv[2], NULL);
Simon Glassbf2e7952017-07-29 11:34:54 -060055
Simon Glassfada3f92022-09-17 09:00:09 -060056 if (blk_print_part_devnum(uclass_id, dev)) {
Simon Glassbf2e7952017-07-29 11:34:54 -060057 printf("\n%s device %d not available\n",
58 if_name, dev);
59 return CMD_RET_FAILURE;
60 }
Bin Meng76f72532023-09-26 16:43:40 +080061 return CMD_RET_SUCCESS;
Simon Glassbf2e7952017-07-29 11:34:54 -060062 }
63 return CMD_RET_USAGE;
64
65 default: /* at least 4 args */
66 if (strcmp(argv[1], "read") == 0) {
Tobias Waldekranz0086a172023-02-16 16:33:48 +010067 phys_addr_t paddr = hextoul(argv[2], NULL);
Simon Glass3ff49ec2021-07-24 09:03:29 -060068 lbaint_t blk = hextoul(argv[3], NULL);
69 ulong cnt = hextoul(argv[4], NULL);
Tobias Waldekranz0086a172023-02-16 16:33:48 +010070 void *vaddr;
Simon Glassbf2e7952017-07-29 11:34:54 -060071 ulong n;
72
Bin Mengbc4ac7e2017-09-12 19:00:36 -070073 printf("\n%s read: device %d block # "LBAFU", count %lu ... ",
74 if_name, *cur_devnump, blk, cnt);
Simon Glassbf2e7952017-07-29 11:34:54 -060075
Tobias Waldekranz0086a172023-02-16 16:33:48 +010076 vaddr = map_sysmem(paddr, 512 * cnt);
Simon Glassfada3f92022-09-17 09:00:09 -060077 n = blk_read_devnum(uclass_id, *cur_devnump, blk, cnt,
Tobias Waldekranz0086a172023-02-16 16:33:48 +010078 vaddr);
79 unmap_sysmem(vaddr);
Simon Glassbf2e7952017-07-29 11:34:54 -060080
81 printf("%ld blocks read: %s\n", n,
82 n == cnt ? "OK" : "ERROR");
Bin Meng76f72532023-09-26 16:43:40 +080083 return n == cnt ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
Simon Glassbf2e7952017-07-29 11:34:54 -060084 } else if (strcmp(argv[1], "write") == 0) {
Tobias Waldekranz0086a172023-02-16 16:33:48 +010085 phys_addr_t paddr = hextoul(argv[2], NULL);
Simon Glass3ff49ec2021-07-24 09:03:29 -060086 lbaint_t blk = hextoul(argv[3], NULL);
87 ulong cnt = hextoul(argv[4], NULL);
Tobias Waldekranz0086a172023-02-16 16:33:48 +010088 void *vaddr;
Simon Glassbf2e7952017-07-29 11:34:54 -060089 ulong n;
90
Bin Mengbc4ac7e2017-09-12 19:00:36 -070091 printf("\n%s write: device %d block # "LBAFU", count %lu ... ",
92 if_name, *cur_devnump, blk, cnt);
Simon Glassbf2e7952017-07-29 11:34:54 -060093
Tobias Waldekranz0086a172023-02-16 16:33:48 +010094 vaddr = map_sysmem(paddr, 512 * cnt);
Simon Glassfada3f92022-09-17 09:00:09 -060095 n = blk_write_devnum(uclass_id, *cur_devnump, blk, cnt,
Tobias Waldekranz0086a172023-02-16 16:33:48 +010096 vaddr);
97 unmap_sysmem(vaddr);
Simon Glassbf2e7952017-07-29 11:34:54 -060098
99 printf("%ld blocks written: %s\n", n,
100 n == cnt ? "OK" : "ERROR");
Bin Meng76f72532023-09-26 16:43:40 +0800101 return n == cnt ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
Simon Glassbf2e7952017-07-29 11:34:54 -0600102 } else {
103 return CMD_RET_USAGE;
104 }
Simon Glassbf2e7952017-07-29 11:34:54 -0600105 }
106}