VirtualBox

source: vbox/trunk/include/VBox/scsiinline.h@ 98818

Last change on this file since 98818 was 98103, checked in by vboxsync, 23 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.7 KB
Line 
1/* $Id: scsiinline.h 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * VirtualBox: SCSI inline helpers used by devices, drivers, etc.
4 */
5
6/*
7 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37#ifndef VBOX_INCLUDED_scsiinline_h
38#define VBOX_INCLUDED_scsiinline_h
39#ifndef RT_WITHOUT_PRAGMA_ONCE
40# pragma once
41#endif
42
43#include <iprt/stdint.h>
44
45/** @defgroup grp_scsi_inline The SCSI inline helpers
46 * @{
47 */
48
49
50/**
51 * Converts a given 16bit value to big endian and stores it in the given buffer.
52 *
53 * @returns nothing.
54 * @param pbBuf The buffer to store the value into.
55 * @param u16Val The value to store.
56 */
57DECLINLINE(void) scsiH2BE_U16(uint8_t *pbBuf, uint16_t u16Val)
58{
59 pbBuf[0] = u16Val >> 8;
60 pbBuf[1] = u16Val;
61}
62
63
64/**
65 * Converts a given 24bit value to big endian and stores it in the given buffer.
66 *
67 * @returns nothing.
68 * @param pbBuf The buffer to store the value into.
69 * @param u32Val The value to store.
70 */
71DECLINLINE(void) scsiH2BE_U24(uint8_t *pbBuf, uint32_t u32Val)
72{
73 pbBuf[0] = u32Val >> 16;
74 pbBuf[1] = u32Val >> 8;
75 pbBuf[2] = u32Val;
76}
77
78
79/**
80 * Converts a given 32bit value to big endian and stores it in the given buffer.
81 *
82 * @returns nothing.
83 * @param pbBuf The buffer to store the value into.
84 * @param u32Val The value to store.
85 */
86DECLINLINE(void) scsiH2BE_U32(uint8_t *pbBuf, uint32_t u32Val)
87{
88 pbBuf[0] = u32Val >> 24;
89 pbBuf[1] = u32Val >> 16;
90 pbBuf[2] = u32Val >> 8;
91 pbBuf[3] = u32Val;
92}
93
94
95/**
96 * Converts a given 64bit value to big endian and stores it in the given buffer.
97 *
98 * @returns nothing.
99 * @param pbBuf The buffer to store the value into.
100 * @param u64Val The value to store.
101 */
102DECLINLINE(void) scsiH2BE_U64(uint8_t *pbBuf, uint64_t u64Val)
103{
104 pbBuf[0] = u64Val >> 56;
105 pbBuf[1] = u64Val >> 48;
106 pbBuf[2] = u64Val >> 40;
107 pbBuf[3] = u64Val >> 32;
108 pbBuf[4] = u64Val >> 24;
109 pbBuf[5] = u64Val >> 16;
110 pbBuf[6] = u64Val >> 8;
111 pbBuf[7] = u64Val;
112}
113
114/**
115 * Returns a 16bit value read from the given buffer converted to host endianess.
116 *
117 * @returns The converted 16bit value.
118 * @param pbBuf The buffer to read the value from.
119 */
120DECLINLINE(uint16_t) scsiBE2H_U16(const uint8_t *pbBuf)
121{
122 return (pbBuf[0] << 8) | pbBuf[1];
123}
124
125
126/**
127 * Returns a 24bit value read from the given buffer converted to host endianess.
128 *
129 * @returns The converted 24bit value as a 32bit unsigned integer.
130 * @param pbBuf The buffer to read the value from.
131 */
132DECLINLINE(uint32_t) scsiBE2H_U24(const uint8_t *pbBuf)
133{
134 return (pbBuf[0] << 16) | (pbBuf[1] << 8) | pbBuf[2];
135}
136
137
138/**
139 * Returns a 32bit value read from the given buffer converted to host endianess.
140 *
141 * @returns The converted 32bit value.
142 * @param pbBuf The buffer to read the value from.
143 */
144DECLINLINE(uint32_t) scsiBE2H_U32(const uint8_t *pbBuf)
145{
146 return (pbBuf[0] << 24) | (pbBuf[1] << 16) | (pbBuf[2] << 8) | pbBuf[3];
147}
148
149
150/**
151 * Returns a 64bit value read from the given buffer converted to host endianess.
152 *
153 * @returns The converted 64bit value.
154 * @param pbBuf The buffer to read the value from.
155 */
156DECLINLINE(uint64_t) scsiBE2H_U64(const uint8_t *pbBuf)
157{
158 return ((uint64_t)pbBuf[0] << 56)
159 | ((uint64_t)pbBuf[1] << 48)
160 | ((uint64_t)pbBuf[2] << 40)
161 | ((uint64_t)pbBuf[3] << 32)
162 | ((uint64_t)pbBuf[4] << 24)
163 | ((uint64_t)pbBuf[5] << 16)
164 | ((uint64_t)pbBuf[6] << 8)
165 | (uint64_t)pbBuf[7];
166}
167
168
169/**
170 * Converts the given LBA number to the MSF (Minutes:Seconds:Frames) format
171 * and stores it in the given buffer.
172 *
173 * @returns nothing.
174 * @param pbBuf The buffer to store the value into.
175 * @param iLBA The LBA to convert.
176 */
177DECLINLINE(void) scsiLBA2MSF(uint8_t *pbBuf, uint32_t iLBA)
178{
179 iLBA += 150;
180 pbBuf[0] = (iLBA / 75) / 60;
181 pbBuf[1] = (iLBA / 75) % 60;
182 pbBuf[2] = iLBA % 75;
183}
184
185
186/**
187 * Converts a MSF formatted address value read from the given buffer
188 * to an LBA number.
189 *
190 * @returns The LBA number.
191 * @param pbBuf The buffer to read the MSF formatted address
192 * from.
193 */
194DECLINLINE(uint32_t) scsiMSF2LBA(const uint8_t *pbBuf)
195{
196 return (pbBuf[0] * 60 + pbBuf[1]) * 75 + pbBuf[2] - 150;
197}
198
199
200/**
201 * Copies a given string to the given destination padding all unused space
202 * in the destination with spaces.
203 *
204 * @returns nothing.
205 * @param pbDst Where to store the string padded with spaces.
206 * @param pbSrc The string to copy.
207 * @param cbSize Size of the destination buffer.
208 */
209DECLINLINE(void) scsiPadStr(uint8_t *pbDst, const char *pbSrc, uint32_t cbSize)
210{
211 uint32_t i;
212 for (i = 0; i < cbSize; i++)
213 {
214 if (*pbSrc)
215 pbDst[i] = *pbSrc++;
216 else
217 pbDst[i] = ' ';
218 }
219}
220
221
222/**
223 * Copies a given string to the given destination padding all unused space
224 * in the destination with spaces.
225 *
226 * @returns nothing.
227 * @param pbDst Where to store the string padded with spaces.
228 * @param pbSrc The string to copy.
229 * @param cbSize Size of the destination buffer.
230 */
231DECLINLINE(void) scsiPadStrS(int8_t *pbDst, const char *pbSrc, uint32_t cbSize)
232{
233 uint32_t i;
234 for (i = 0; i < cbSize; i++)
235 {
236 if (*pbSrc)
237 pbDst[i] = *pbSrc++;
238 else
239 pbDst[i] = ' ';
240 }
241}
242
243/** @} */
244
245#endif /* !VBOX_INCLUDED_scsiinline_h */
246
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette