3 * Copyright (C) 2012 secunet Security Networks AG
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <libpayload.h>
33 #include <storage/ata.h>
34 #include <storage/atapi.h>
36 static int atapi_request_sense(atapi_dev_t
*const dev
)
39 cdb
[0] = ATAPI_REQUEST_SENSE
;
40 cdb
[4] = sizeof(dev
->sense_data
);
41 return dev
->packet_read_cmd(dev
, cdb
, sizeof(cdb
),
42 dev
->sense_data
, sizeof(dev
->sense_data
));
45 static ssize_t
atapi_packet_read_cmd(atapi_dev_t
*const dev
,
46 const u8
*const cmd
, const size_t cmdlen
,
47 u8
*const buf
, const size_t buflen
)
53 ret
= dev
->packet_read_cmd(dev
, cmd
, cmdlen
, buf
, buflen
);
57 ret
= atapi_request_sense(dev
);
59 printf("atapi: Requesting sense failed.\n");
63 switch (dev
->sense_data
[2] & 0xf) {
64 case ATAPI_SENSE_UNIT_ATTENTION
:
67 case ATAPI_SENSE_NOT_READY
:
68 switch (dev
->sense_data
[12]) {
69 case ATAPI_ADDITIONAL_SENSE_LOGICAL_UNIT_NOT_READY
:
70 /* Device wants more time. */
73 case ATAPI_ADDITIONAL_SENSE_MEDIUM_NOT_PRESENT
:
74 printf("atapi: No medium present.\n");
75 dev
->medium_present
= 0;
84 /* No more retries. */
88 static ssize_t
atapi_packet_read_sectors(ata_dev_t
*const _dev
,
89 const lba_t start
, size_t count
,
92 atapi_dev_t
*const dev
= (atapi_dev_t
*)_dev
;
94 if (start
>= (1ULL << 32)) {
95 printf("atapi: Sector is not 32-bit addressable.\n");
97 } else if (count
>= (64 * 1024)) {
98 printf("ahci: Sector count too high (max. 65535).\n");
99 count
= (64 * 1024) - 1;
103 cdb
[0] = ATAPI_READ_10
;
104 cdb
[2] = (start
>> 24) & 0xff;
105 cdb
[3] = (start
>> 16) & 0xff;
106 cdb
[4] = (start
>> 8) & 0xff;
107 cdb
[5] = (start
>> 0) & 0xff;
108 cdb
[7] = (count
>> 8) & 0xff;
109 cdb
[8] = (count
>> 0) & 0xff;
110 const ssize_t ret
= atapi_packet_read_cmd(dev
, cdb
, sizeof(cdb
),
111 buf
, count
<< dev
->ata_dev
.sector_size_shift
);
115 return ret
>> dev
->ata_dev
.sector_size_shift
;
118 static storage_poll_t
atapi_poll(storage_dev_t
*const _dev
)
120 atapi_dev_t
*const dev
= (atapi_dev_t
*)_dev
;
125 if (dev
->medium_present
)
126 return POLL_MEDIUM_PRESENT
;
128 cdb
[0] = ATAPI_TEST_UNIT_READY
;
129 const int ret
= atapi_packet_read_cmd(dev
, cdb
, sizeof(cdb
), NULL
, 0);
131 printf("atapi: Found medium.\n");
132 dev
->medium_present
= 1;
133 } else if (dev
->medium_present
) {
137 if (dev
->medium_present
) {
138 cdb
[0] = ATAPI_START_STOP_UNIT
;
139 cdb
[4] = 0x01; /* Start Disc, read TOC. */
140 if (atapi_packet_read_cmd(dev
, cdb
, sizeof(cdb
), NULL
, 0))
145 cdb
[0] = ATAPI_PREVENT_ALLOW_MEDIUM_REMOVAL
;
146 cdb
[2] = 0x02; /* Clear persistent prevent removal bit. */
147 atapi_packet_read_cmd(dev
, cdb
, sizeof(cdb
), NULL
, 0);
148 cdb
[2] = 0x00; /* Clear prevent removal bit. */
149 atapi_packet_read_cmd(dev
, cdb
, sizeof(cdb
), NULL
, 0);
151 /* If we don't have a medium, we're done. */
152 if (!dev
->medium_present
)
153 return POLL_NO_MEDIUM
;
155 /* Read capacity and sector size. */
156 cdb
[0] = ATAPI_READ_CAPACITY
;
157 if (atapi_packet_read_cmd(dev
, cdb
, sizeof(cdb
),
158 (u8
*)capacity
, sizeof(capacity
))
161 const u32 sector_size
= (capacity
[4] << 24) |
162 (capacity
[5] << 16) |
165 if (ata_set_sector_size(&dev
->ata_dev
, sector_size
)) {
166 dev
->medium_present
= 0;
167 return POLL_MEDIUM_ERROR
;
170 return POLL_MEDIUM_PRESENT
;
173 dev
->medium_present
= 0;
177 int atapi_attach_device(atapi_dev_t
*const dev
, const storage_port_t port_type
)
181 dev
->ata_dev
.identify_cmd
= ATA_IDENTIFY_PACKET_DEVICE
;
182 if (dev
->identify(&dev
->ata_dev
, (u8
*)id
))
185 char fw
[9], model
[41];
186 ata_strncpy(fw
, id
+ 23, sizeof(fw
));
187 ata_strncpy(model
, id
+ 27, sizeof(model
));
188 printf("atapi: Identified %s [%s]\n", model
, fw
);
190 /* Initialize to sane values. */
191 dev
->ata_dev
.sector_size
= 2048;
192 dev
->ata_dev
.sector_size_shift
= 11;
194 dev
->medium_present
= 0;
196 /* Reuse ata procedures. */
197 ata_initialize_storage_ops(&dev
->ata_dev
);
199 dev
->ata_dev
.read_sectors
= atapi_packet_read_sectors
;
200 dev
->ata_dev
.storage_dev
.port_type
= port_type
;
201 dev
->ata_dev
.storage_dev
.poll
= atapi_poll
;
203 return storage_attach_device(&dev
->ata_dev
.storage_dev
);