xref: /aosp_15_r20/external/coreboot/payloads/libpayload/drivers/storage/ahci_atapi.c (revision b9411a12aaaa7e1e6a6fb7c5e057f44ee179a49c)
1 /*
2  *
3  * Copyright (C) 2012 secunet Security Networks AG
4  * Copyright (C) 2013 Edward O'Callaghan <[email protected]>
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 <stdlib.h>
31 #include <stdint.h>
32 #include <string.h>
33 #include <libpayload.h>
34 #include <pci.h>
35 #include <storage/ata.h>
36 #include <storage/ahci.h>
37 
38 #include "ahci_private.h"
39 
ahci_packet_read_cmd(atapi_dev_t * const _dev,const u8 * const cmd,const size_t cmdlen,u8 * const buf,const size_t buflen)40 ssize_t ahci_packet_read_cmd(atapi_dev_t *const _dev,
41 				    const u8 *const cmd, const size_t cmdlen,
42 				    u8 *const buf, const size_t buflen)
43 {
44 	ahci_dev_t *const dev = (ahci_dev_t *)_dev;
45 
46 	if ((cmdlen != 12) && (cmdlen != 16)) {
47 		printf("ahci: Only 12- and 16-byte packet commands allowed.\n");
48 		return -1;
49 	}
50 
51 	const size_t len = ahci_cmdslot_prepare(dev, buf, buflen, 0);
52 	u16 byte_limit = MIN(len, 63 * 1024); /* like Linux */
53 	if (byte_limit & 1) ++byte_limit; /* even limit */
54 
55 	dev->cmdlist[0].cmd |= CMD_ATAPI;
56 	dev->cmdtable->fis[0] = FIS_HOST_TO_DEVICE;
57 	dev->cmdtable->fis[1] = FIS_H2D_CMD;
58 	dev->cmdtable->fis[2] = ATA_PACKET;
59 	dev->cmdtable->fis[5] = byte_limit & 0xff;
60 	dev->cmdtable->fis[6] = byte_limit >> 8;
61 	memcpy((void *)dev->cmdtable->atapi_cmd, cmd, cmdlen);
62 
63 	return ahci_cmdslot_exec(dev);
64 }
65