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
29 #include <libpayload.h>
34 #include <storage/ata.h>
36 /** Reads non-sector-aligned blocks of 512 bytes. */
37 static ssize_t
ata_read_unaligned(ata_dev_t
*const dev
,
38 const lba_t blk_start
, size_t blk_count
,
45 u8
*const sec_buf
= malloc(dev
->sector_size
);
49 const size_t shift
= dev
->sector_size_shift
- 9;
50 const size_t mask
= (dev
->sector_size
>> 9) - 1;
52 /* Sector aligned start block. */
53 const lba_t blk_aligned
= blk_start
& ~mask
;
55 /* First sector to read from. */
56 sec_start
= blk_aligned
>> shift
;
58 /* Calculate and read residue before sector aligned blocks. */
59 blk_residue
= MIN(blk_start
- blk_aligned
, blk_count
);
61 if (dev
->read_sectors(dev
, sec_start
, 1, sec_buf
) != 1)
64 const size_t bytes
= blk_residue
<< 9;
65 memcpy(blk_buf
, sec_buf
+ (dev
->sector_size
- bytes
), bytes
);
66 blk_count
-= blk_residue
;
75 /* Read all sector aligned blocks. */
76 const size_t sec_count
= (blk_count
& ~mask
) >> shift
;
77 const int sec_read
= dev
->read_sectors(
78 dev
, sec_start
, sec_count
, blk_buf
);
81 ret
+= sec_read
<< shift
;
82 if (sec_read
!= sec_count
)
85 /* Calculate and read residue. */
86 blk_residue
= blk_count
& mask
;
88 sec_start
+= sec_read
;
89 blk_buf
+= sec_read
<< dev
->sector_size_shift
;
91 if (dev
->read_sectors(dev
, sec_start
, 1, sec_buf
) != 1)
94 const size_t bytes
= blk_residue
<< 9;
95 memcpy(blk_buf
, sec_buf
, bytes
);
104 static ssize_t
ata_read512(storage_dev_t
*_dev
,
105 const lba_t start
, const size_t count
,
106 unsigned char *const buf
)
108 ata_dev_t
*const dev
= (ata_dev_t
*)_dev
;
110 if (dev
->read_sectors
== NULL
) {
111 printf("ata: No read support implemented.\n");
115 if (dev
->sector_size
== 512) {
116 return dev
->read_sectors(dev
, start
, count
, buf
);
117 } else if (dev
->sector_size
> 512) {
118 /* Sector size has to be a power of two. */
119 const size_t mask
= (dev
->sector_size
>> 9) - 1;
120 if (!(start
& mask
) && !(count
& mask
)) {
121 const size_t shift
= dev
->sector_size_shift
- 9;
122 const ssize_t ret
= dev
->read_sectors(dev
,
123 start
>> shift
, count
>> shift
, buf
);
129 return ata_read_unaligned(dev
, start
, count
, buf
);
132 printf("ata: No support for sectors smaller than 512 bytes.\n");
137 static ssize_t
ata_write512(storage_dev_t
*const dev
,
138 const lba_t start
, const size_t count
,
139 const unsigned char *const buf
)
141 printf("ata: No write support implemented.\n");
145 void ata_initialize_storage_ops(ata_dev_t
*const dev
)
147 dev
->storage_dev
.read_blocks512
= ata_read512
;
148 dev
->storage_dev
.write_blocks512
= ata_write512
;
151 int ata_set_sector_size(ata_dev_t
*const dev
, u32 sector_size
)
153 if (!sector_size
|| (sector_size
& (sector_size
- 1))) {
154 printf("ata: Sector size is not a power of two (%u).\n",
158 dev
->sector_size
= sector_size
;
159 dev
->sector_size_shift
= 0;
160 while (sector_size
>>= 1)
161 ++dev
->sector_size_shift
;
166 static int ata_decode_sector_size(ata_dev_t
*const dev
, const u16
*const id
)
169 if ((id
[ATA_ID_SECTOR_SIZE
] & ((3 << 14) | (1 << 12)))
170 != ((1 << 14) | (1 << 12)))
171 size
= DEFAULT_ATA_SECTOR_SIZE
;
173 size
= (id
[ATA_ID_LOGICAL_SECTOR_SIZE
] |
174 (id
[ATA_ID_LOGICAL_SECTOR_SIZE
+ 1] << 16)) << 1;
176 return ata_set_sector_size(dev
, size
);
180 * Copies n-1 bytes from src to dest swapping each two bytes, removes
181 * trailing spaces and terminates dest with '\0'.
183 char *ata_strncpy(char *const dest
, const u16
*const src
, const size_t n
)
187 for (i
= 0; i
< (n
- 1); i
+= 2) {
188 dest
[i
] = ((const char *)src
)[i
+ 1];
189 dest
[i
+ 1] = ((const char *)src
)[i
];
192 for (i
= n
- 2; i
>= 0; --i
)
200 int ata_attach_device(ata_dev_t
*const dev
, const storage_port_t port_type
)
204 dev
->identify_cmd
= ATA_IDENTIFY_DEVICE
;
205 if (dev
->identify(dev
, (u8
*)id
))
208 char fw
[9], model
[41];
209 ata_strncpy(fw
, id
+ 23, sizeof(fw
));
210 ata_strncpy(model
, id
+ 27, sizeof(model
));
211 printf("ata: Identified %s [%s]\n", model
, fw
);
213 #if CONFIG(LP_STORAGE_64BIT_LBA)
214 if (id
[ATA_CMDS_AND_FEATURE_SETS
+ 1] & (1 << 10)) {
215 printf("ata: Support for LBA-48 enabled.\n");
216 dev
->read_cmd
= ATA_READ_DMA_EXT
;
218 dev
->read_cmd
= ATA_READ_DMA
;
221 dev
->read_cmd
= ATA_READ_DMA
;
224 if (ata_decode_sector_size(dev
, id
))
227 dev
->storage_dev
.port_type
= port_type
;
228 ata_initialize_storage_ops(dev
);
230 return storage_attach_device(&dev
->storage_dev
);