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