blob: 570971891ec5c994908f1a235f240b8180d0d5eb [file] [log] [blame]
wdenke887afc2002-08-27 09:44:07 +00001/*
2 * (C) Copyright 2001
3 * Denis Peter, MPL AG Switzerland
4 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenke887afc2002-08-27 09:44:07 +00006 */
7
8/*
9 * SCSI support.
10 */
wdenke887afc2002-08-27 09:44:07 +000011#include <common.h>
12#include <command.h>
wdenke887afc2002-08-27 09:44:07 +000013#include <scsi.h>
wdenke887afc2002-08-27 09:44:07 +000014
15static int scsi_curr_dev; /* current device */
16
Simon Glassc8e89ab2016-05-01 11:36:09 -060017/*
wdenke887afc2002-08-27 09:44:07 +000018 * scsi boot command intepreter. Derived from diskboot
19 */
Michal Simekff238112016-11-30 12:18:36 +010020static int do_scsiboot(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
wdenke887afc2002-08-27 09:44:07 +000021{
Rob Herringa20cbf92012-09-21 04:02:30 +000022 return common_diskboot(cmdtp, "scsi", argc, argv);
wdenke887afc2002-08-27 09:44:07 +000023}
24
Simon Glassc8e89ab2016-05-01 11:36:09 -060025/*
wdenke887afc2002-08-27 09:44:07 +000026 * scsi command intepreter
27 */
Michal Simekff238112016-11-30 12:18:36 +010028static int do_scsi(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
wdenke887afc2002-08-27 09:44:07 +000029{
Michal Simek5dc196f2016-11-30 12:12:31 +010030 int ret;
31
wdenke887afc2002-08-27 09:44:07 +000032 switch (argc) {
Simon Glassa06dfc72011-12-10 08:44:01 +000033 case 0:
34 case 1:
35 return CMD_RET_USAGE;
Simon Glassa06dfc72011-12-10 08:44:01 +000036 case 2:
Simon Glass5c7bf8e2016-05-01 11:35:58 -060037 if (strncmp(argv[1], "res", 3) == 0) {
38 printf("\nReset SCSI\n");
Simon Glass11b2b622017-06-14 21:28:40 -060039 scsi_bus_reset(NULL);
Simon Glass48228732017-06-14 21:28:41 -060040 ret = scsi_scan(true);
Michal Simek5dc196f2016-11-30 12:12:31 +010041 if (ret)
42 return CMD_RET_FAILURE;
43 return ret;
Simon Glass5c7bf8e2016-05-01 11:35:58 -060044 }
45 if (strncmp(argv[1], "inf", 3) == 0) {
Simon Glassc8e89ab2016-05-01 11:36:09 -060046 blk_list_devices(IF_TYPE_SCSI);
Simon Glass5c7bf8e2016-05-01 11:35:58 -060047 return 0;
48 }
49 if (strncmp(argv[1], "dev", 3) == 0) {
Simon Glassc8e89ab2016-05-01 11:36:09 -060050 if (blk_print_device_num(IF_TYPE_SCSI, scsi_curr_dev)) {
Simon Glass5c7bf8e2016-05-01 11:35:58 -060051 printf("\nno SCSI devices available\n");
Simon Glassc8e89ab2016-05-01 11:36:09 -060052 return CMD_RET_FAILURE;
wdenke887afc2002-08-27 09:44:07 +000053 }
Simon Glassc8e89ab2016-05-01 11:36:09 -060054
Simon Glass5c7bf8e2016-05-01 11:35:58 -060055 return 0;
56 }
57 if (strncmp(argv[1], "scan", 4) == 0) {
Simon Glass48228732017-06-14 21:28:41 -060058 ret = scsi_scan(true);
Michal Simek5dc196f2016-11-30 12:12:31 +010059 if (ret)
60 return CMD_RET_FAILURE;
61 return ret;
Simon Glass5c7bf8e2016-05-01 11:35:58 -060062 }
63 if (strncmp(argv[1], "part", 4) == 0) {
Simon Glassc8e89ab2016-05-01 11:36:09 -060064 if (blk_list_part(IF_TYPE_SCSI))
Simon Glass5c7bf8e2016-05-01 11:35:58 -060065 printf("\nno SCSI devices available\n");
Simon Glassc8e89ab2016-05-01 11:36:09 -060066 return 0;
Simon Glass5c7bf8e2016-05-01 11:35:58 -060067 }
68 return CMD_RET_USAGE;
69 case 3:
70 if (strncmp(argv[1], "dev", 3) == 0) {
71 int dev = (int)simple_strtoul(argv[2], NULL, 10);
Simon Glassc8e89ab2016-05-01 11:36:09 -060072
73 if (!blk_show_device(IF_TYPE_SCSI, dev)) {
74 scsi_curr_dev = dev;
75 printf("... is now current device\n");
76 } else {
77 return CMD_RET_FAILURE;
Simon Glass8f81fa92016-05-01 11:35:57 -060078 }
Simon Glass5c7bf8e2016-05-01 11:35:58 -060079 return 0;
80 }
81 if (strncmp(argv[1], "part", 4) == 0) {
82 int dev = (int)simple_strtoul(argv[2], NULL, 10);
Simon Glassc8e89ab2016-05-01 11:36:09 -060083
84 if (blk_print_part_devnum(IF_TYPE_SCSI, dev)) {
85 printf("\nSCSI device %d not available\n",
86 dev);
87 return CMD_RET_FAILURE;
88 }
89 return 0;
Simon Glass5c7bf8e2016-05-01 11:35:58 -060090 }
91 return CMD_RET_USAGE;
92 default:
93 /* at least 4 args */
94 if (strcmp(argv[1], "read") == 0) {
95 ulong addr = simple_strtoul(argv[2], NULL, 16);
96 ulong blk = simple_strtoul(argv[3], NULL, 16);
97 ulong cnt = simple_strtoul(argv[4], NULL, 16);
98 ulong n;
Simon Glassc8e89ab2016-05-01 11:36:09 -060099
Simon Glass5c7bf8e2016-05-01 11:35:58 -0600100 printf("\nSCSI read: device %d block # %ld, count %ld ... ",
101 scsi_curr_dev, blk, cnt);
Simon Glassc8e89ab2016-05-01 11:36:09 -0600102 n = blk_read_devnum(IF_TYPE_SCSI, scsi_curr_dev, blk,
103 cnt, (ulong *)addr);
Simon Glass5c7bf8e2016-05-01 11:35:58 -0600104 printf("%ld blocks read: %s\n", n,
105 n == cnt ? "OK" : "ERROR");
106 return 0;
107 } else if (strcmp(argv[1], "write") == 0) {
108 ulong addr = simple_strtoul(argv[2], NULL, 16);
109 ulong blk = simple_strtoul(argv[3], NULL, 16);
110 ulong cnt = simple_strtoul(argv[4], NULL, 16);
111 ulong n;
Simon Glassc8e89ab2016-05-01 11:36:09 -0600112
Simon Glass5c7bf8e2016-05-01 11:35:58 -0600113 printf("\nSCSI write: device %d block # %ld, count %ld ... ",
114 scsi_curr_dev, blk, cnt);
Simon Glassc8e89ab2016-05-01 11:36:09 -0600115 n = blk_write_devnum(IF_TYPE_SCSI, scsi_curr_dev, blk,
116 cnt, (ulong *)addr);
Simon Glass5c7bf8e2016-05-01 11:35:58 -0600117 printf("%ld blocks written: %s\n", n,
118 n == cnt ? "OK" : "ERROR");
119 return 0;
120 }
Simon Glass8f81fa92016-05-01 11:35:57 -0600121 } /* switch */
122 return CMD_RET_USAGE;
Hung-Te Lin98a6c4e2012-10-29 05:23:46 +0000123}
124
Simon Glass8f81fa92016-05-01 11:35:57 -0600125U_BOOT_CMD(
126 scsi, 5, 1, do_scsi,
127 "SCSI sub-system",
128 "reset - reset SCSI controller\n"
129 "scsi info - show available SCSI devices\n"
130 "scsi scan - (re-)scan SCSI bus\n"
131 "scsi device [dev] - show or set current device\n"
132 "scsi part [dev] - print partition table of one or all SCSI devices\n"
133 "scsi read addr blk# cnt - read `cnt' blocks starting at block `blk#'\n"
134 " to memory address `addr'\n"
135 "scsi write addr blk# cnt - write `cnt' blocks starting at block\n"
136 " `blk#' from memory address `addr'"
137);
138
139U_BOOT_CMD(
140 scsiboot, 3, 1, do_scsiboot,
141 "boot from SCSI device",
142 "loadAddr dev:part"
143);