xref: /openbmc/u-boot/cmd/scsi.c (revision c2012cb4)
1 /*
2  * (C) Copyright 2001
3  * Denis Peter, MPL AG Switzerland
4  *
5  * SPDX-License-Identifier:	GPL-2.0+
6  */
7 
8 /*
9  * SCSI support.
10  */
11 #include <common.h>
12 #include <command.h>
13 #include <scsi.h>
14 
15 static int scsi_curr_dev; /* current device */
16 
17 /*
18  * scsi boot command intepreter. Derived from diskboot
19  */
20 static int do_scsiboot(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
21 {
22 	return common_diskboot(cmdtp, "scsi", argc, argv);
23 }
24 
25 /*
26  * scsi command intepreter
27  */
28 static int do_scsi(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
29 {
30 	int ret;
31 
32 	switch (argc) {
33 	case 0:
34 	case 1:
35 		return CMD_RET_USAGE;
36 	case 2:
37 		if (strncmp(argv[1], "res", 3) == 0) {
38 			printf("\nReset SCSI\n");
39 #ifndef CONFIG_DM_SCSI
40 			scsi_bus_reset(NULL);
41 #endif
42 			ret = scsi_scan(true);
43 			if (ret)
44 				return CMD_RET_FAILURE;
45 			return ret;
46 		}
47 		if (strncmp(argv[1], "inf", 3) == 0) {
48 			blk_list_devices(IF_TYPE_SCSI);
49 			return 0;
50 		}
51 		if (strncmp(argv[1], "dev", 3) == 0) {
52 			if (blk_print_device_num(IF_TYPE_SCSI, scsi_curr_dev)) {
53 				printf("\nno SCSI devices available\n");
54 				return CMD_RET_FAILURE;
55 			}
56 
57 			return 0;
58 		}
59 		if (strncmp(argv[1], "scan", 4) == 0) {
60 			ret = scsi_scan(true);
61 			if (ret)
62 				return CMD_RET_FAILURE;
63 			return ret;
64 		}
65 		if (strncmp(argv[1], "part", 4) == 0) {
66 			if (blk_list_part(IF_TYPE_SCSI))
67 				printf("\nno SCSI devices available\n");
68 			return 0;
69 		}
70 		return CMD_RET_USAGE;
71 	case 3:
72 		if (strncmp(argv[1], "dev", 3) == 0) {
73 			int dev = (int)simple_strtoul(argv[2], NULL, 10);
74 
75 			if (!blk_show_device(IF_TYPE_SCSI, dev)) {
76 				scsi_curr_dev = dev;
77 				printf("... is now current device\n");
78 			} else {
79 				return CMD_RET_FAILURE;
80 			}
81 			return 0;
82 		}
83 		if (strncmp(argv[1], "part", 4) == 0) {
84 			int dev = (int)simple_strtoul(argv[2], NULL, 10);
85 
86 			if (blk_print_part_devnum(IF_TYPE_SCSI, dev)) {
87 				printf("\nSCSI device %d not available\n",
88 				       dev);
89 				return CMD_RET_FAILURE;
90 			}
91 			return 0;
92 		}
93 		return CMD_RET_USAGE;
94 	default:
95 		/* at least 4 args */
96 		if (strcmp(argv[1], "read") == 0) {
97 			ulong addr = simple_strtoul(argv[2], NULL, 16);
98 			ulong blk  = simple_strtoul(argv[3], NULL, 16);
99 			ulong cnt  = simple_strtoul(argv[4], NULL, 16);
100 			ulong n;
101 
102 			printf("\nSCSI read: device %d block # %ld, count %ld ... ",
103 			       scsi_curr_dev, blk, cnt);
104 			n = blk_read_devnum(IF_TYPE_SCSI, scsi_curr_dev, blk,
105 					    cnt, (ulong *)addr);
106 			printf("%ld blocks read: %s\n", n,
107 			       n == cnt ? "OK" : "ERROR");
108 			return 0;
109 		} else if (strcmp(argv[1], "write") == 0) {
110 			ulong addr = simple_strtoul(argv[2], NULL, 16);
111 			ulong blk = simple_strtoul(argv[3], NULL, 16);
112 			ulong cnt = simple_strtoul(argv[4], NULL, 16);
113 			ulong n;
114 
115 			printf("\nSCSI write: device %d block # %ld, count %ld ... ",
116 			       scsi_curr_dev, blk, cnt);
117 			n = blk_write_devnum(IF_TYPE_SCSI, scsi_curr_dev, blk,
118 					     cnt, (ulong *)addr);
119 			printf("%ld blocks written: %s\n", n,
120 			       n == cnt ? "OK" : "ERROR");
121 			return 0;
122 		}
123 	} /* switch */
124 	return CMD_RET_USAGE;
125 }
126 
127 U_BOOT_CMD(
128 	scsi, 5, 1, do_scsi,
129 	"SCSI sub-system",
130 	"reset - reset SCSI controller\n"
131 	"scsi info  - show available SCSI devices\n"
132 	"scsi scan  - (re-)scan SCSI bus\n"
133 	"scsi device [dev] - show or set current device\n"
134 	"scsi part [dev] - print partition table of one or all SCSI devices\n"
135 	"scsi read addr blk# cnt - read `cnt' blocks starting at block `blk#'\n"
136 	"     to memory address `addr'\n"
137 	"scsi write addr blk# cnt - write `cnt' blocks starting at block\n"
138 	"     `blk#' from memory address `addr'"
139 );
140 
141 U_BOOT_CMD(
142 	scsiboot, 3, 1, do_scsiboot,
143 	"boot from SCSI device",
144 	"loadAddr dev:part"
145 );
146