1 // Copyright (C) 2014 Michael McMaster <michael@codesrc.com>
3 // This file is part of SCSI2SD.
5 // SCSI2SD is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // SCSI2SD is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with SCSI2SD. If not, see <http://www.gnu.org/licenses/>.
18 #include "ConfigUtil.hh"
25 using namespace SCSI2SD;
29 // Endian conversion routines.
30 // The Cortex-M3 inside the Cypress PSoC 5LP is a
31 // little-endian device.
44 uint16_t toLE16(uint16_t in)
52 return (in >> 8) | (in << 8);
55 uint16_t fromLE16(uint16_t in)
60 uint32_t toLE32(uint32_t in)
69 ((in >> 8) & 0xff00) |
70 ((in << 8) & 0xff0000) |
74 uint32_t fromLE32(uint32_t in)
82 ConfigUtil::Default(size_t targetIdx)
85 memset(&config, 0, sizeof(config));
87 config.scsiId = targetIdx;
90 config.scsiId = config.scsiId | CONFIG_TARGET_ENABLED;
92 config.deviceType = CONFIG_FIXED;
93 config.flags = CONFIG_ENABLE_PARITY | CONFIG_ENABLE_UNIT_ATTENTION;
95 config.sdSectorStart = 0;
96 config.scsiSectors = 2147483648; // 1TB
97 config.bytesPerSector = 512;
98 config.sectorsPerTrack = 63;
99 config.headsPerCylinder = 255;
100 memcpy(config.vendor, " codesrc", 8);
101 memcpy(config.prodId, " SCSI2SD", 16);
102 memcpy(config.revision, " 4.0", 4);
103 memcpy(config.serial, "1234567812345678", 16);
105 // Reserved fields, already set to 0
108 // not supported yet.
116 ConfigUtil::fromBytes(const uint8_t* data)
119 memcpy(&result, data, sizeof(TargetConfig));
120 result.sdSectorStart = toLE32(result.sdSectorStart);
121 result.scsiSectors = toLE32(result.scsiSectors);
122 result.bytesPerSector = toLE16(result.bytesPerSector);
123 result.sectorsPerTrack = toLE16(result.sectorsPerTrack);
124 result.headsPerCylinder = toLE16(result.headsPerCylinder);
130 ConfigUtil::toBytes(const TargetConfig& _config)
132 TargetConfig config(_config);
133 config.sdSectorStart = fromLE32(config.sdSectorStart);
134 config.scsiSectors = fromLE32(config.scsiSectors);
135 config.bytesPerSector = fromLE16(config.bytesPerSector);
136 config.sectorsPerTrack = fromLE16(config.sectorsPerTrack);
137 config.headsPerCylinder = fromLE16(config.headsPerCylinder);
139 const uint8_t* begin = reinterpret_cast<const uint8_t*>(&config);
140 return std::vector<uint8_t>(begin, begin + sizeof(config));