Coverage Report

Created: 2026-10-31 08:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qemu/hw/block/cdrom.c
Line
Count
Source
1
/*
2
 * QEMU ATAPI CD-ROM Emulator
3
 *
4
 * Copyright (c) 2006 Fabrice Bellard
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to deal
8
 * in the Software without restriction, including without limitation the rights
9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
 * copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
 * THE SOFTWARE.
23
 */
24
25
/* ??? Most of the ATAPI emulation is still in ide.c.  It should be moved
26
   here.  */
27
28
#include "qemu/osdep.h"
29
#include "qemu/bswap.h"
30
#include "hw/scsi/scsi.h"
31
32
static void lba_to_msf(uint8_t *buf, int lba)
33
661
{
34
661
    lba += 150;
35
661
    buf[0] = (lba / 75) / 60;
36
661
    buf[1] = (lba / 75) % 60;
37
661
    buf[2] = lba % 75;
38
661
}
39
40
/* same toc as bochs. Return -1 if error or the toc length */
41
/* XXX: check this */
42
int cdrom_read_toc(int nb_sectors, uint8_t *buf, int msf, int start_track)
43
1.66k
{
44
1.66k
    uint8_t *q;
45
1.66k
    int len;
46
47
1.66k
    if (start_track > 1 && start_track != 0xaa)
48
504
        return -1;
49
1.15k
    q = buf + 2;
50
1.15k
    *q++ = 1; /* first session */
51
1.15k
    *q++ = 1; /* last session */
52
1.15k
    if (start_track <= 1) {
53
979
        *q++ = 0; /* reserved */
54
979
        *q++ = 0x14; /* ADR, control */
55
979
        *q++ = 1;    /* track number */
56
979
        *q++ = 0; /* reserved */
57
979
        if (msf) {
58
242
            *q++ = 0; /* reserved */
59
242
            lba_to_msf(q, 0);
60
242
            q += 3;
61
737
        } else {
62
            /* sector 0 */
63
737
            stl_be_p(q, 0);
64
737
            q += 4;
65
737
        }
66
979
    }
67
    /* lead out track */
68
1.15k
    *q++ = 0; /* reserved */
69
1.15k
    *q++ = 0x16; /* ADR, control */
70
1.15k
    *q++ = 0xaa; /* track number */
71
1.15k
    *q++ = 0; /* reserved */
72
1.15k
    if (msf) {
73
419
        *q++ = 0; /* reserved */
74
419
        lba_to_msf(q, nb_sectors);
75
419
        q += 3;
76
737
    } else {
77
737
        stl_be_p(q, nb_sectors);
78
737
        q += 4;
79
737
    }
80
1.15k
    len = q - buf;
81
1.15k
    stw_be_p(buf, len - 2);
82
1.15k
    return len;
83
1.66k
}
84
85
/* mostly same info as PearPc */
86
int cdrom_read_toc_raw(int nb_sectors, uint8_t *buf, int msf, int session_num)
87
0
{
88
0
    uint8_t *q;
89
0
    int len;
90
91
0
    q = buf + 2;
92
0
    *q++ = 1; /* first session */
93
0
    *q++ = 1; /* last session */
94
95
0
    *q++ = 1; /* session number */
96
0
    *q++ = 0x14; /* data track */
97
0
    *q++ = 0; /* track number */
98
0
    *q++ = 0xa0; /* lead-in */
99
0
    *q++ = 0; /* min */
100
0
    *q++ = 0; /* sec */
101
0
    *q++ = 0; /* frame */
102
0
    *q++ = 0;
103
0
    *q++ = 1; /* first track */
104
0
    *q++ = 0x00; /* disk type */
105
0
    *q++ = 0x00;
106
107
0
    *q++ = 1; /* session number */
108
0
    *q++ = 0x14; /* data track */
109
0
    *q++ = 0; /* track number */
110
0
    *q++ = 0xa1;
111
0
    *q++ = 0; /* min */
112
0
    *q++ = 0; /* sec */
113
0
    *q++ = 0; /* frame */
114
0
    *q++ = 0;
115
0
    *q++ = 1; /* last track */
116
0
    *q++ = 0x00;
117
0
    *q++ = 0x00;
118
119
0
    *q++ = 1; /* session number */
120
0
    *q++ = 0x14; /* data track */
121
0
    *q++ = 0; /* track number */
122
0
    *q++ = 0xa2; /* lead-out */
123
0
    *q++ = 0; /* min */
124
0
    *q++ = 0; /* sec */
125
0
    *q++ = 0; /* frame */
126
0
    if (msf) {
127
0
        *q++ = 0; /* reserved */
128
0
        lba_to_msf(q, nb_sectors);
129
0
        q += 3;
130
0
    } else {
131
0
        stl_be_p(q, nb_sectors);
132
0
        q += 4;
133
0
    }
134
135
0
    *q++ = 1; /* session number */
136
0
    *q++ = 0x14; /* ADR, control */
137
0
    *q++ = 0;    /* track number */
138
0
    *q++ = 1;    /* point */
139
0
    *q++ = 0; /* min */
140
0
    *q++ = 0; /* sec */
141
0
    *q++ = 0; /* frame */
142
0
    if (msf) {
143
0
        *q++ = 0;
144
0
        lba_to_msf(q, 0);
145
0
        q += 3;
146
0
    } else {
147
0
        *q++ = 0;
148
0
        *q++ = 0;
149
0
        *q++ = 0;
150
0
        *q++ = 0;
151
0
    }
152
153
0
    len = q - buf;
154
0
    stw_be_p(buf, len - 2);
155
0
    return len;
156
0
}