Nico Huber | 1f6bd94 | 2012-08-30 15:36:57 +0200 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the libpayload project. |
| 3 | * |
| 4 | * Copyright (C) 2012 secunet Security Networks AG |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions |
| 8 | * are met: |
| 9 | * 1. Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * 3. The name of the author may not be used to endorse or promote products |
| 15 | * derived from this software without specific prior written permission. |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 27 | * SUCH DAMAGE. |
| 28 | */ |
| 29 | |
| 30 | #include <stdint.h> |
| 31 | #include <stdio.h> |
| 32 | #include <libpayload.h> |
| 33 | |
| 34 | #include <storage/ata.h> |
| 35 | #include <storage/atapi.h> |
| 36 | |
| 37 | |
| 38 | static int atapi_request_sense(atapi_dev_t *const dev) |
| 39 | { |
| 40 | u8 cdb[12] = { 0, }; |
| 41 | cdb[0] = ATAPI_REQUEST_SENSE; |
| 42 | cdb[4] = sizeof(dev->sense_data); |
| 43 | return dev->packet_read_cmd(dev, cdb, sizeof(cdb), |
| 44 | dev->sense_data, sizeof(dev->sense_data)); |
| 45 | } |
| 46 | |
| 47 | static ssize_t atapi_packet_read_cmd(atapi_dev_t *const dev, |
| 48 | const u8 *const cmd, const size_t cmdlen, |
| 49 | u8 *const buf, const size_t buflen) |
| 50 | { |
| 51 | int retries = 10; |
| 52 | ssize_t ret; |
| 53 | |
| 54 | do { |
| 55 | ret = dev->packet_read_cmd(dev, cmd, cmdlen, buf, buflen); |
| 56 | if (ret >= 0) |
| 57 | return ret; |
| 58 | |
| 59 | ret = atapi_request_sense(dev); |
| 60 | if (ret < 0) { |
| 61 | printf("atapi: Requesting sense failed.\n"); |
| 62 | return -1; |
| 63 | } |
| 64 | |
| 65 | switch (dev->sense_data[2] & 0xf) { |
| 66 | case ATAPI_SENSE_UNIT_ATTENTION: |
| 67 | /* Nothing to do. */ |
| 68 | break; |
| 69 | case ATAPI_SENSE_NOT_READY: |
| 70 | switch (dev->sense_data[12]) { |
| 71 | case ATAPI_ADDITIONAL_SENSE_LOGICAL_UNIT_NOT_READY: |
| 72 | /* Device wants more time. */ |
| 73 | delay(3); |
| 74 | break; |
| 75 | case ATAPI_ADDITIONAL_SENSE_MEDIUM_NOT_PRESENT: |
| 76 | printf("atapi: No medium present.\n"); |
| 77 | dev->medium_present = 0; |
| 78 | return -1; |
| 79 | } |
| 80 | break; |
| 81 | default: |
| 82 | return -1; |
| 83 | } |
| 84 | } while (retries--); |
| 85 | |
| 86 | /* No more retries. */ |
| 87 | return -1; |
| 88 | } |
| 89 | |
| 90 | static ssize_t atapi_packet_read_sectors(ata_dev_t *const _dev, |
| 91 | const lba_t start, size_t count, |
| 92 | u8 *const buf) |
| 93 | { |
| 94 | atapi_dev_t *const dev = (atapi_dev_t *)_dev; |
| 95 | |
| 96 | if (start >= (1ULL << 32)) { |
| 97 | printf("atapi: Sector is not 32-bit addressable.\n"); |
| 98 | return -1; |
| 99 | } else if (count >= (64 * 1024)) { |
| 100 | printf("ahci: Sector count too high (max. 65535).\n"); |
| 101 | count = (64 * 1024) - 1; |
| 102 | } |
| 103 | |
| 104 | u8 cdb[12] = { 0, }; |
| 105 | cdb[0] = ATAPI_READ_10; |
| 106 | cdb[2] = (start >> 24) & 0xff; |
| 107 | cdb[3] = (start >> 16) & 0xff; |
| 108 | cdb[4] = (start >> 8) & 0xff; |
| 109 | cdb[5] = (start >> 0) & 0xff; |
| 110 | cdb[7] = (count >> 8) & 0xff; |
| 111 | cdb[8] = (count >> 0) & 0xff; |
| 112 | const ssize_t ret = atapi_packet_read_cmd(dev, cdb, sizeof(cdb), |
| 113 | buf, count << dev->ata_dev.sector_size_shift); |
| 114 | if (ret < 0) |
| 115 | return ret; |
| 116 | else |
| 117 | return ret >> dev->ata_dev.sector_size_shift; |
| 118 | } |
| 119 | |
| 120 | static storage_poll_t atapi_poll(storage_dev_t *const _dev) |
| 121 | { |
| 122 | atapi_dev_t *const dev = (atapi_dev_t *)_dev; |
| 123 | |
| 124 | u8 cdb[12] = { 0, }; |
| 125 | u8 capacity[8]; |
| 126 | |
| 127 | if (dev->medium_present) |
| 128 | return POLL_MEDIUM_PRESENT; |
| 129 | |
| 130 | cdb[0] = ATAPI_TEST_UNIT_READY; |
| 131 | const int ret = atapi_packet_read_cmd(dev, cdb, sizeof(cdb), NULL, 0); |
| 132 | if (!ret) { |
| 133 | printf("atapi: Found medium.\n"); |
| 134 | dev->medium_present = 1; |
| 135 | } else if (dev->medium_present) { |
| 136 | return POLL_ERROR; |
| 137 | } |
| 138 | |
| 139 | if (dev->medium_present) { |
| 140 | cdb[0] = ATAPI_START_STOP_UNIT; |
| 141 | cdb[4] = 0x01; /* Start Disc, read TOC. */ |
| 142 | if (atapi_packet_read_cmd(dev, cdb, sizeof(cdb), NULL, 0)) |
| 143 | goto _error_ret; |
| 144 | cdb[4] = 0x00; |
| 145 | } |
| 146 | |
| 147 | cdb[0] = ATAPI_PREVENT_ALLOW_MEDIUM_REMOVAL; |
| 148 | cdb[2] = 0x02; /* Clear persistent prevent removal bit. */ |
| 149 | atapi_packet_read_cmd(dev, cdb, sizeof(cdb), NULL, 0); |
| 150 | cdb[2] = 0x00; /* Clear prevent removal bit. */ |
| 151 | atapi_packet_read_cmd(dev, cdb, sizeof(cdb), NULL, 0); |
| 152 | |
| 153 | /* If we don't have a medium, we're done. */ |
| 154 | if (!dev->medium_present) |
| 155 | return POLL_NO_MEDIUM; |
| 156 | |
| 157 | /* Read capacity and sector size. */ |
| 158 | cdb[0] = ATAPI_READ_CAPACITY; |
| 159 | if (atapi_packet_read_cmd(dev, cdb, sizeof(cdb), |
| 160 | (u8 *)capacity, sizeof(capacity)) |
| 161 | != sizeof(capacity)) |
| 162 | goto _error_ret; |
| 163 | const u32 sector_size = (capacity[4] << 24) | |
| 164 | (capacity[5] << 16) | |
| 165 | (capacity[6] << 8) | |
| 166 | (capacity[7] << 0); |
| 167 | if (ata_set_sector_size(&dev->ata_dev, sector_size)) { |
| 168 | dev->medium_present = 0; |
| 169 | return POLL_MEDIUM_ERROR; |
| 170 | } |
| 171 | |
| 172 | return POLL_MEDIUM_PRESENT; |
| 173 | |
| 174 | _error_ret: |
| 175 | dev->medium_present = 0; |
| 176 | return POLL_ERROR; |
| 177 | } |
| 178 | |
| 179 | int atapi_attach_device(atapi_dev_t *const dev, const storage_port_t port_type) |
| 180 | { |
| 181 | u16 id[256]; |
| 182 | |
| 183 | dev->ata_dev.identify_cmd = ATA_IDENTIFY_PACKET_DEVICE; |
| 184 | if (dev->identify(&dev->ata_dev, (u8 *)id)) |
| 185 | return -1; |
| 186 | |
| 187 | char fw[9], model[41]; |
| 188 | ata_strncpy(fw, id + 23, sizeof(fw)); |
| 189 | ata_strncpy(model, id + 27, sizeof(model)); |
| 190 | printf("atapi: Identified %s [%s]\n", model, fw); |
| 191 | |
| 192 | /* Initialize to sane values. */ |
| 193 | dev->ata_dev.sector_size = 2048; |
| 194 | dev->ata_dev.sector_size_shift = 11; |
| 195 | |
| 196 | dev->medium_present = 0; |
| 197 | |
| 198 | /* Reuse ata procedures. */ |
| 199 | ata_initialize_storage_ops(&dev->ata_dev); |
| 200 | |
| 201 | dev->ata_dev.read_sectors = atapi_packet_read_sectors; |
| 202 | dev->ata_dev.storage_dev.port_type = port_type; |
| 203 | dev->ata_dev.storage_dev.poll = atapi_poll; |
| 204 | |
| 205 | return storage_attach_device(&dev->ata_dev.storage_dev); |
| 206 | } |