Move common "command data block" functions to new file blockcmd.c.
[seabios.git] / src / blockcmd.c
1 // Support for several common scsi like command data block requests
2 //
3 // Copyright (C) 2010  Kevin O'Connor <kevin@koconnor.net>
4 // Copyright (C) 2002  MandrakeSoft S.A.
5 //
6 // This file may be distributed under the terms of the GNU LGPLv3 license.
7
8 #include "biosvar.h" // GET_GLOBAL
9 #include "util.h" // htonl
10 #include "disk.h" // struct disk_op_s
11 #include "blockcmd.h" // struct cdb_request_sense
12 #include "ata.h" // atapi_cmd_data
13
14 static int
15 cdb_cmd_data(struct disk_op_s *op, void *cdbcmd, u16 blocksize)
16 {
17     u8 type = GET_GLOBAL(op->drive_g->type);
18     switch (type) {
19     case DTYPE_ATAPI:
20         return atapi_cmd_data(op, cdbcmd, blocksize);
21     default:
22         op->count = 0;
23         return DISK_RET_EPARAM;
24     }
25 }
26
27 // Request SENSE
28 int
29 cdb_get_sense(struct disk_op_s *op, struct cdbres_request_sense *data)
30 {
31     struct cdb_request_sense cmd;
32     memset(&cmd, 0, sizeof(cmd));
33     cmd.command = CDB_CMD_REQUEST_SENSE;
34     cmd.length = sizeof(*data);
35     op->count = 1;
36     op->buf_fl = data;
37     return cdb_cmd_data(op, &cmd, sizeof(*data));
38 }
39
40 // Request capacity
41 int
42 cdb_read_capacity(struct disk_op_s *op, struct cdbres_read_capacity *data)
43 {
44     struct cdb_read_capacity cmd;
45     memset(&cmd, 0, sizeof(cmd));
46     cmd.command = CDB_CMD_READ_CAPACITY;
47     op->count = 1;
48     op->buf_fl = data;
49     return cdb_cmd_data(op, &cmd, sizeof(*data));
50 }
51
52 // Read sectors.
53 int
54 cdb_read(struct disk_op_s *op)
55 {
56     struct cdb_rwdata_10 cmd;
57     memset(&cmd, 0, sizeof(cmd));
58     cmd.command = CDB_CMD_READ_10;
59     cmd.lba = htonl(op->lba);
60     cmd.count = htons(op->count);
61     return cdb_cmd_data(op, &cmd, CDROM_SECTOR_SIZE);
62 }