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"
23 #include <arpa/inet.h>
26 using namespace SCSI2SD;
29 ConfigUtil::Default(size_t targetIdx)
32 memset(&config, 0, sizeof(config));
34 config.scsiId = targetIdx;
37 config.scsiId = config.scsiId | CONFIG_TARGET_ENABLED;
39 config.deviceType = CONFIG_FIXED;
40 config.flags = CONFIG_ENABLE_PARITY | CONFIG_ENABLE_UNIT_ATTENTION;
42 config.sdSectorStart = 0;
43 config.scsiSectors = std::numeric_limits<uint32_t>::max();
44 config.bytesPerSector = 512;
45 config.sectorsPerTrack = 63;
46 config.headsPerCylinder = 255;
47 memcpy(config.vendor, " codesrc", 8);
48 memcpy(config.prodId, " SCSI2SD", 16);
49 memcpy(config.revision, " 3.6", 4);
50 memcpy(config.serial, "1234567812345678", 16);
52 // Reserved fields, already set to 0
63 ConfigUtil::fromBytes(const uint8_t* data)
66 memcpy(&result, data, sizeof(TargetConfig));
67 result.sdSectorStart = ntohl(result.sdSectorStart);
68 result.scsiSectors = ntohl(result.scsiSectors);
69 result.bytesPerSector = ntohs(result.bytesPerSector);
70 result.sectorsPerTrack = ntohs(result.sectorsPerTrack);
71 result.headsPerCylinder = ntohs(result.headsPerCylinder);
77 ConfigUtil::toBytes(const TargetConfig& _config)
79 TargetConfig config(_config);
80 config.sdSectorStart = htonl(config.sdSectorStart);
81 config.scsiSectors = htonl(config.scsiSectors);
82 config.bytesPerSector = htons(config.bytesPerSector);
83 config.sectorsPerTrack = htons(config.sectorsPerTrack);
84 config.headsPerCylinder = htons(config.headsPerCylinder);
86 const uint8_t* begin = reinterpret_cast<const uint8_t*>(&config);
87 return std::vector<uint8_t>(begin, begin + sizeof(config));