• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0+
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.
9  */
10 
11 #include <common.h>
12 #include <blk.h>
13 
blk_common_cmd(int argc,char * const argv[],enum if_type if_type,int * cur_devnump)14 int blk_common_cmd(int argc, char * const argv[], enum if_type if_type,
15 		   int *cur_devnump)
16 {
17 	const char *if_name = blk_get_if_type_name(if_type);
18 
19 	switch (argc) {
20 	case 0:
21 	case 1:
22 		return CMD_RET_USAGE;
23 	case 2:
24 		if (strncmp(argv[1], "inf", 3) == 0) {
25 			blk_list_devices(if_type);
26 			return 0;
27 		} else if (strncmp(argv[1], "dev", 3) == 0) {
28 			if (blk_print_device_num(if_type, *cur_devnump)) {
29 				printf("\nno %s devices available\n", if_name);
30 				return CMD_RET_FAILURE;
31 			}
32 			return 0;
33 		} else if (strncmp(argv[1], "part", 4) == 0) {
34 			if (blk_list_part(if_type))
35 				printf("\nno %s devices available\n", if_name);
36 			return 0;
37 		}
38 		return CMD_RET_USAGE;
39 	case 3:
40 		if (strncmp(argv[1], "dev", 3) == 0) {
41 			int dev = (int)simple_strtoul(argv[2], NULL, 10);
42 
43 			if (!blk_show_device(if_type, dev)) {
44 				*cur_devnump = dev;
45 				printf("... is now current device\n");
46 			} else {
47 				return CMD_RET_FAILURE;
48 			}
49 			return 0;
50 		} else if (strncmp(argv[1], "part", 4) == 0) {
51 			int dev = (int)simple_strtoul(argv[2], NULL, 10);
52 
53 			if (blk_print_part_devnum(if_type, dev)) {
54 				printf("\n%s device %d not available\n",
55 				       if_name, dev);
56 				return CMD_RET_FAILURE;
57 			}
58 			return 0;
59 		}
60 		return CMD_RET_USAGE;
61 
62 	default: /* at least 4 args */
63 		if (strcmp(argv[1], "read") == 0) {
64 			ulong addr = simple_strtoul(argv[2], NULL, 16);
65 			lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
66 			ulong cnt = simple_strtoul(argv[4], NULL, 16);
67 			ulong n;
68 
69 			printf("\n%s read: device %d block # "LBAFU", count %lu ... ",
70 			       if_name, *cur_devnump, blk, cnt);
71 
72 			n = blk_read_devnum(if_type, *cur_devnump, blk, cnt,
73 					    (ulong *)addr);
74 
75 			printf("%ld blocks read: %s\n", n,
76 			       n == cnt ? "OK" : "ERROR");
77 			return n == cnt ? 0 : 1;
78 		} else if (strcmp(argv[1], "write") == 0) {
79 			ulong addr = simple_strtoul(argv[2], NULL, 16);
80 			lbaint_t blk = simple_strtoul(argv[3], NULL, 16);
81 			ulong cnt = simple_strtoul(argv[4], NULL, 16);
82 			ulong n;
83 
84 			printf("\n%s write: device %d block # "LBAFU", count %lu ... ",
85 			       if_name, *cur_devnump, blk, cnt);
86 
87 			n = blk_write_devnum(if_type, *cur_devnump, blk, cnt,
88 					     (ulong *)addr);
89 
90 			printf("%ld blocks written: %s\n", n,
91 			       n == cnt ? "OK" : "ERROR");
92 			return n == cnt ? 0 : 1;
93 		} else {
94 			return CMD_RET_USAGE;
95 		}
96 	}
97 }
98