Edward O'Callaghan (eocallaghan@alterapraxis.com) just uploaded a new patch set to gerrit, which you can find at http://review.coreboot.org/4783
-gerrit
commit a2cebf2f36987d79c4ce61461b285e9fbcb11ca4 Author: Edward O'Callaghan eocallaghan@alterapraxis.com Date: Thu Jan 23 08:30:42 2014 +1100
Fix warnings in libpayload, decompartmentalises AHCI driver.
Decompartmentalise AHCI driver and add a few superficial comments while here.
Change-Id: Ia1fd545b39868a81cbc311f6ffc786f9f1f61415 Signed-off-by: Edward O'Callaghan eocallaghan@alterapraxis.com --- payloads/libpayload/drivers/Makefile.inc | 4 + payloads/libpayload/drivers/storage/ahci.c | 106 +-------------------- .../drivers/storage/ahci_ata_read_sectors.c | 100 +++++++++++++++++++ .../drivers/storage/ahci_identify_device.c | 56 +++++++++++ .../drivers/storage/ahci_packet_read_cmd.c | 67 +++++++++++++ payloads/libpayload/drivers/storage/ahci_private.h | 21 +++- payloads/libpayload/libc/hexdump.c | 2 +- 7 files changed, 250 insertions(+), 106 deletions(-)
diff --git a/payloads/libpayload/drivers/Makefile.inc b/payloads/libpayload/drivers/Makefile.inc index a1891be..1db0b7a 100644 --- a/payloads/libpayload/drivers/Makefile.inc +++ b/payloads/libpayload/drivers/Makefile.inc @@ -54,10 +54,14 @@ libc-$(CONFIG_GEODELX_VIDEO_CONSOLE) += video/font8x16.c libc-$(CONFIG_COREBOOT_VIDEO_CONSOLE) += video/corebootfb.c libc-$(CONFIG_COREBOOT_VIDEO_CONSOLE) += video/font8x16.c
+# AHCI/ATAPI driver libc-$(CONFIG_STORAGE) += storage/storage.c libc-$(CONFIG_STORAGE_ATA) += storage/ata.c libc-$(CONFIG_STORAGE_ATAPI) += storage/atapi.c libc-$(CONFIG_STORAGE_AHCI) += storage/ahci.c +libc-$(CONFIG_STORAGE_AHCI) += storage/ahci_ata_read_sectors.c +libc-$(CONFIG_STORAGE_AHCI) += storage/ahci_identify_device.c +libc-$(CONFIG_STORAGE_AHCI) += storage/ahci_packet_read_cmd.c
# USB stack libc-$(CONFIG_USB) += usb/usbinit.c diff --git a/payloads/libpayload/drivers/storage/ahci.c b/payloads/libpayload/drivers/storage/ahci.c index ec4db4c..6d8abff 100644 --- a/payloads/libpayload/drivers/storage/ahci.c +++ b/payloads/libpayload/drivers/storage/ahci.c @@ -189,7 +189,7 @@ static void ahci_prdbuf_finalize(ahci_dev_t *const dev) dev->buflen = 0; }
-static ssize_t ahci_cmdslot_exec(ahci_dev_t *const dev) +ssize_t ahci_cmdslot_exec(ahci_dev_t *const dev) { const int slotnum = 0; /* We always use the first slot. */
@@ -221,7 +221,7 @@ static ssize_t ahci_cmdslot_exec(ahci_dev_t *const dev) } }
-static size_t ahci_cmdslot_prepare(ahci_dev_t *const dev, +size_t ahci_cmdslot_prepare(ahci_dev_t *const dev, u8 *const user_buf, size_t buf_len, const int out) { @@ -268,108 +268,6 @@ static size_t ahci_cmdslot_prepare(ahci_dev_t *const dev, return read_count; }
-static ssize_t ahci_ata_read_sectors(ata_dev_t *const ata_dev, - const lba_t start, size_t count, - u8 *const buf) -{ - ahci_dev_t *const dev = (ahci_dev_t *)ata_dev; - - if (count == 0) - return 0; - - if (ata_dev->read_cmd == ATA_READ_DMA) { - if (start >= (1 << 28)) { - printf("ahci: Sector is not 28-bit addressable.\n"); - return -1; - } else if (count > 256) { - printf("ahci: Sector count too high (max. 256).\n"); - count = 256; - } -#ifdef CONFIG_STORAGE_64BIT_LBA - } else if (ata_dev->read_cmd == ATA_READ_DMA_EXT) { - if (start >= (1ULL << 48)) { - printf("ahci: Sector is not 48-bit addressable.\n"); - return -1; - } else if (count > (64 * 1024)) { - printf("ahci: Sector count too high (max. 65536).\n"); - count = 64 * 1024; - } -#endif - } else { - printf("ahci: Unsupported ATA read command (0x%x).\n", - ata_dev->read_cmd); - return -1; - } - - const size_t bytes = count << ata_dev->sector_size_shift; - const size_t bytes_feasible = ahci_cmdslot_prepare(dev, buf, bytes, 0); - const size_t sectors = bytes_feasible >> ata_dev->sector_size_shift; - - dev->cmdtable->fis[ 0] = FIS_HOST_TO_DEVICE; - dev->cmdtable->fis[ 1] = FIS_H2D_CMD; - dev->cmdtable->fis[ 2] = ata_dev->read_cmd; - dev->cmdtable->fis[ 4] = (start >> 0) & 0xff; - dev->cmdtable->fis[ 5] = (start >> 8) & 0xff; - dev->cmdtable->fis[ 6] = (start >> 16) & 0xff; - dev->cmdtable->fis[ 7] = FIS_H2D_DEV_LBA; - dev->cmdtable->fis[ 8] = (start >> 24) & 0xff; -#ifdef CONFIG_STORAGE_64BIT_LBA - if (ata_dev->read_cmd == ATA_READ_DMA_EXT) { - dev->cmdtable->fis[ 9] = (start >> 32) & 0xff; - dev->cmdtable->fis[10] = (start >> 40) & 0xff; - } -#endif - dev->cmdtable->fis[12] = (sectors >> 0) & 0xff; - dev->cmdtable->fis[13] = (sectors >> 8) & 0xff; - - if (ahci_cmdslot_exec(dev) < 0) - return -1; - else - return dev->cmdlist->prd_bytes >> ata_dev->sector_size_shift; -} - -static ssize_t ahci_packet_read_cmd(atapi_dev_t *const _dev, - const u8 *const cmd, const size_t cmdlen, - u8 *const buf, const size_t buflen) -{ - ahci_dev_t *const dev = (ahci_dev_t *)_dev; - - if ((cmdlen != 12) && (cmdlen != 16)) { - printf("ahci: Only 12- and 16-byte packet commands allowed.\n"); - return -1; - } - - const size_t len = ahci_cmdslot_prepare(dev, buf, buflen, 0); - u16 byte_limit = MIN(len, 63 * 1024); /* like Linux */ - if (byte_limit & 1) ++byte_limit; /* even limit */ - - dev->cmdlist[0].cmd |= CMD_ATAPI; - dev->cmdtable->fis[0] = FIS_HOST_TO_DEVICE; - dev->cmdtable->fis[1] = FIS_H2D_CMD; - dev->cmdtable->fis[2] = ATA_PACKET; - dev->cmdtable->fis[5] = byte_limit & 0xff; - dev->cmdtable->fis[6] = byte_limit >> 8; - memcpy((void *)dev->cmdtable->atapi_cmd, cmd, cmdlen); - - return ahci_cmdslot_exec(dev); -} - -static int ahci_identify_device(ata_dev_t *const ata_dev, u8 *const buf) -{ - ahci_dev_t *const dev = (ahci_dev_t *)ata_dev; - - ahci_cmdslot_prepare(dev, buf, 512, 0); - - dev->cmdtable->fis[0] = FIS_HOST_TO_DEVICE; - dev->cmdtable->fis[1] = FIS_H2D_CMD; - dev->cmdtable->fis[2] = ata_dev->identify_cmd; - - if ((ahci_cmdslot_exec(dev) < 0) || (dev->cmdlist->prd_bytes != 512)) - return -1; - else - return 0; -} - static int ahci_dev_init(hba_ctrl_t *const ctrl, hba_port_t *const port, const int portnum) diff --git a/payloads/libpayload/drivers/storage/ahci_ata_read_sectors.c b/payloads/libpayload/drivers/storage/ahci_ata_read_sectors.c new file mode 100644 index 0000000..1efec23 --- /dev/null +++ b/payloads/libpayload/drivers/storage/ahci_ata_read_sectors.c @@ -0,0 +1,100 @@ +/* + * This file is part of the libpayload project. + * + * Copyright (C) 2012 secunet Security Networks AG + * Copyright (C) 2013 Edward O'Callaghan eocallaghan@alterapraxis.com + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <stdlib.h> +#include <stdint.h> +#include <string.h> +#include <libpayload.h> +#include <pci.h> +#include <storage/ata.h> +#include <storage/ahci.h> + +#include "ahci_private.h" + + +ssize_t ahci_ata_read_sectors(ata_dev_t *const ata_dev, + const lba_t start, size_t count, + u8 *const buf) +{ + ahci_dev_t *const dev = (ahci_dev_t *)ata_dev; + + if (count == 0) + return 0; + + if (ata_dev->read_cmd == ATA_READ_DMA) { + if (start >= (1 << 28)) { + printf("ahci: Sector is not 28-bit addressable.\n"); + return -1; + } else if (count > 256) { + printf("ahci: Sector count too high (max. 256).\n"); + count = 256; + } +#ifdef CONFIG_STORAGE_64BIT_LBA + } else if (ata_dev->read_cmd == ATA_READ_DMA_EXT) { + if (start >= (1ULL << 48)) { + printf("ahci: Sector is not 48-bit addressable.\n"); + return -1; + } else if (count > (64 * 1024)) { + printf("ahci: Sector count too high (max. 65536).\n"); + count = 64 * 1024; + } +#endif + } else { + printf("ahci: Unsupported ATA read command (0x%x).\n", + ata_dev->read_cmd); + return -1; + } + + const size_t bytes = count << ata_dev->sector_size_shift; + const size_t bytes_feasible = ahci_cmdslot_prepare(dev, buf, bytes, 0); + const size_t sectors = bytes_feasible >> ata_dev->sector_size_shift; + + dev->cmdtable->fis[ 0] = FIS_HOST_TO_DEVICE; + dev->cmdtable->fis[ 1] = FIS_H2D_CMD; + dev->cmdtable->fis[ 2] = ata_dev->read_cmd; + dev->cmdtable->fis[ 4] = (start >> 0) & 0xff; + dev->cmdtable->fis[ 5] = (start >> 8) & 0xff; + dev->cmdtable->fis[ 6] = (start >> 16) & 0xff; + dev->cmdtable->fis[ 7] = FIS_H2D_DEV_LBA; + dev->cmdtable->fis[ 8] = (start >> 24) & 0xff; +#ifdef CONFIG_STORAGE_64BIT_LBA + if (ata_dev->read_cmd == ATA_READ_DMA_EXT) { + dev->cmdtable->fis[ 9] = (start >> 32) & 0xff; + dev->cmdtable->fis[10] = (start >> 40) & 0xff; + } +#endif + dev->cmdtable->fis[12] = (sectors >> 0) & 0xff; + dev->cmdtable->fis[13] = (sectors >> 8) & 0xff; + + if (ahci_cmdslot_exec(dev) < 0) + return -1; + else + return dev->cmdlist->prd_bytes >> ata_dev->sector_size_shift; +} diff --git a/payloads/libpayload/drivers/storage/ahci_identify_device.c b/payloads/libpayload/drivers/storage/ahci_identify_device.c new file mode 100644 index 0000000..51812c2 --- /dev/null +++ b/payloads/libpayload/drivers/storage/ahci_identify_device.c @@ -0,0 +1,56 @@ +/* + * This file is part of the libpayload project. + * + * Copyright (C) 2012 secunet Security Networks AG + * Copyright (C) 2013 Edward O'Callaghan eocallaghan@alterapraxis.com + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <stdlib.h> +#include <stdint.h> +#include <string.h> +#include <libpayload.h> +#include <pci.h> +#include <storage/ata.h> +#include <storage/ahci.h> + +#include "ahci_private.h" + + +int ahci_identify_device(ata_dev_t *const ata_dev, u8 *const buf) +{ + ahci_dev_t *const dev = (ahci_dev_t *)ata_dev; + + ahci_cmdslot_prepare(dev, buf, 512, 0); + + dev->cmdtable->fis[0] = FIS_HOST_TO_DEVICE; + dev->cmdtable->fis[1] = FIS_H2D_CMD; + dev->cmdtable->fis[2] = ata_dev->identify_cmd; + + if ((ahci_cmdslot_exec(dev) < 0) || (dev->cmdlist->prd_bytes != 512)) + return -1; + else + return 0; +} diff --git a/payloads/libpayload/drivers/storage/ahci_packet_read_cmd.c b/payloads/libpayload/drivers/storage/ahci_packet_read_cmd.c new file mode 100644 index 0000000..11305c2 --- /dev/null +++ b/payloads/libpayload/drivers/storage/ahci_packet_read_cmd.c @@ -0,0 +1,67 @@ +/* + * This file is part of the libpayload project. + * + * Copyright (C) 2012 secunet Security Networks AG + * Copyright (C) 2013 Edward O'Callaghan eocallaghan@alterapraxis.com + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <stdlib.h> +#include <stdint.h> +#include <string.h> +#include <libpayload.h> +#include <pci.h> +#include <storage/ata.h> +#include <storage/ahci.h> + + +#include "ahci_private.h" + + +ssize_t ahci_packet_read_cmd(atapi_dev_t *const _dev, + const u8 *const cmd, const size_t cmdlen, + u8 *const buf, const size_t buflen) +{ + ahci_dev_t *const dev = (ahci_dev_t *)_dev; + + if ((cmdlen != 12) && (cmdlen != 16)) { + printf("ahci: Only 12- and 16-byte packet commands allowed.\n"); + return -1; + } + + const size_t len = ahci_cmdslot_prepare(dev, buf, buflen, 0); + u16 byte_limit = MIN(len, 63 * 1024); /* like Linux */ + if (byte_limit & 1) ++byte_limit; /* even limit */ + + dev->cmdlist[0].cmd |= CMD_ATAPI; + dev->cmdtable->fis[0] = FIS_HOST_TO_DEVICE; + dev->cmdtable->fis[1] = FIS_H2D_CMD; + dev->cmdtable->fis[2] = ATA_PACKET; + dev->cmdtable->fis[5] = byte_limit & 0xff; + dev->cmdtable->fis[6] = byte_limit >> 8; + memcpy((void *)dev->cmdtable->atapi_cmd, cmd, cmdlen); + + return ahci_cmdslot_exec(dev); +} diff --git a/payloads/libpayload/drivers/storage/ahci_private.h b/payloads/libpayload/drivers/storage/ahci_private.h index ba1471b..25d0f10 100644 --- a/payloads/libpayload/drivers/storage/ahci_private.h +++ b/payloads/libpayload/drivers/storage/ahci_private.h @@ -2,6 +2,7 @@ * This file is part of the libpayload project. * * Copyright (C) 2012 secunet Security Networks AG + * Copyright (C) 2013 Edward O'Callaghan eocallaghan@alterapraxis.com * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -198,4 +199,22 @@ typedef struct { size_t buflen; } ahci_dev_t;
-#endif +ssize_t ahci_cmdslot_exec(ahci_dev_t *const dev); + +size_t ahci_cmdslot_prepare(ahci_dev_t *const dev, + u8 *const user_buf, size_t buf_len, + const int out); + + +ssize_t ahci_ata_read_sectors(ata_dev_t *const ata_dev, + const lba_t start, size_t count, + u8 *const buf); + +ssize_t ahci_packet_read_cmd(atapi_dev_t *const _dev, + const u8 *const cmd, const size_t cmdlen, + u8 *const buf, const size_t buflen); + +int ahci_identify_device(ata_dev_t *const ata_dev, u8 *const buf); + + +#endif /* _AHCI_PRIVATE_H */ diff --git a/payloads/libpayload/libc/hexdump.c b/payloads/libpayload/libc/hexdump.c index e4aa8b9..814adf2 100644 --- a/payloads/libpayload/libc/hexdump.c +++ b/payloads/libpayload/libc/hexdump.c @@ -49,7 +49,7 @@ void hexdump(void *memory, int length) }
if (all_zero < 2) { - printf("%08lx:", memory + i); + printf("%8p:", memory + i); for (j = 0; j < 16; j++) printf(" %02x", m[i + j]); printf(" ");