blob: 7442e6aef4d9d06a2efcb0eb55ed5d4a416a0db3 [file] [log] [blame]
wdenke887afc2002-08-27 09:44:07 +00001/*
2 * (C) Copyright 2001
3 * Denis Peter, MPL AG Switzerland
4 *
Wolfgang Denk1a459662013-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 Glass11f610e2016-05-01 11:36:09 -060017/*
wdenke887afc2002-08-27 09:44:07 +000018 * scsi boot command intepreter. Derived from diskboot
19 */
Simon Glass11f610e2016-05-01 11:36:09 -060020int do_scsiboot(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
wdenke887afc2002-08-27 09:44:07 +000021{
Rob Herring7405a132012-09-21 04:02:30 +000022 return common_diskboot(cmdtp, "scsi", argc, argv);
wdenke887afc2002-08-27 09:44:07 +000023}
24
Simon Glass11f610e2016-05-01 11:36:09 -060025/*
wdenke887afc2002-08-27 09:44:07 +000026 * scsi command intepreter
27 */
Simon Glass11f610e2016-05-01 11:36:09 -060028int do_scsi(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
wdenke887afc2002-08-27 09:44:07 +000029{
Michal Simekc002e392016-11-30 12:12:31 +010030 int ret;
31
wdenke887afc2002-08-27 09:44:07 +000032 switch (argc) {
Simon Glass4c12eeb2011-12-10 08:44:01 +000033 case 0:
34 case 1:
35 return CMD_RET_USAGE;
Simon Glass4c12eeb2011-12-10 08:44:01 +000036 case 2:
Simon Glassf1d4d932016-05-01 11:35:58 -060037 if (strncmp(argv[1], "res", 3) == 0) {
38 printf("\nReset SCSI\n");
39 scsi_bus_reset();
Michal Simekc002e392016-11-30 12:12:31 +010040 ret = scsi_scan(1);
41 if (ret)
42 return CMD_RET_FAILURE;
43 return ret;
Simon Glassf1d4d932016-05-01 11:35:58 -060044 }
45 if (strncmp(argv[1], "inf", 3) == 0) {
Simon Glass11f610e2016-05-01 11:36:09 -060046 blk_list_devices(IF_TYPE_SCSI);
Simon Glassf1d4d932016-05-01 11:35:58 -060047 return 0;
48 }
49 if (strncmp(argv[1], "dev", 3) == 0) {
Simon Glass11f610e2016-05-01 11:36:09 -060050 if (blk_print_device_num(IF_TYPE_SCSI, scsi_curr_dev)) {
Simon Glassf1d4d932016-05-01 11:35:58 -060051 printf("\nno SCSI devices available\n");
Simon Glass11f610e2016-05-01 11:36:09 -060052 return CMD_RET_FAILURE;
wdenke887afc2002-08-27 09:44:07 +000053 }
Simon Glass11f610e2016-05-01 11:36:09 -060054
Simon Glassf1d4d932016-05-01 11:35:58 -060055 return 0;
56 }
57 if (strncmp(argv[1], "scan", 4) == 0) {
Michal Simekc002e392016-11-30 12:12:31 +010058 ret = scsi_scan(1);
59 if (ret)
60 return CMD_RET_FAILURE;
61 return ret;
Simon Glassf1d4d932016-05-01 11:35:58 -060062 }
63 if (strncmp(argv[1], "part", 4) == 0) {
Simon Glass11f610e2016-05-01 11:36:09 -060064 if (blk_list_part(IF_TYPE_SCSI))
Simon Glassf1d4d932016-05-01 11:35:58 -060065 printf("\nno SCSI devices available\n");
Simon Glass11f610e2016-05-01 11:36:09 -060066 return 0;
Simon Glassf1d4d932016-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 Glass11f610e2016-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;
wdenke887afc2002-08-27 09:44:07 +000078 }
Simon Glassf1d4d932016-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 Glass11f610e2016-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 Glassf1d4d932016-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 Glass11f610e2016-05-01 11:36:09 -060099
Simon Glassf1d4d932016-05-01 11:35:58 -0600100 printf("\nSCSI read: device %d block # %ld, count %ld ... ",
101 scsi_curr_dev, blk, cnt);
Simon Glass11f610e2016-05-01 11:36:09 -0600102 n = blk_read_devnum(IF_TYPE_SCSI, scsi_curr_dev, blk,
103 cnt, (ulong *)addr);
Simon Glassf1d4d932016-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 Glass11f610e2016-05-01 11:36:09 -0600112
Simon Glassf1d4d932016-05-01 11:35:58 -0600113 printf("\nSCSI write: device %d block # %ld, count %ld ... ",
114 scsi_curr_dev, blk, cnt);
Simon Glass11f610e2016-05-01 11:36:09 -0600115 n = blk_write_devnum(IF_TYPE_SCSI, scsi_curr_dev, blk,
116 cnt, (ulong *)addr);
Simon Glassf1d4d932016-05-01 11:35:58 -0600117 printf("%ld blocks written: %s\n", n,
118 n == cnt ? "OK" : "ERROR");
119 return 0;
120 }
wdenke887afc2002-08-27 09:44:07 +0000121 } /* switch */
Simon Glass4c12eeb2011-12-10 08:44:01 +0000122 return CMD_RET_USAGE;
wdenke887afc2002-08-27 09:44:07 +0000123}
124
Tom Riniba524262016-03-16 09:45:03 -0400125U_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);