Lots of bug fixing, and 4.7 merges
[SCSI2SD-V6.git] / src / scsi2sd-util6 / ConfigUtil.cc
1 //      Copyright (C) 2014 Michael McMaster <michael@codesrc.com>
2 //
3 //      This file is part of SCSI2SD.
4 //
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.
9 //
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.
14 //
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/>.
17
18 #include "ConfigUtil.hh"
19
20 #include <limits>
21 #include <sstream>
22 #include <stdexcept>
23
24 #include <string.h>
25
26 #include <wx/wxprec.h>
27 #ifndef WX_PRECOMP
28 #include <wx/wx.h>
29 #endif
30 #include <wx/base64.h>
31 #include <wx/buffer.h>
32 #include <wx/xml/xml.h>
33
34
35 using namespace SCSI2SD;
36
37 namespace
38 {
39         // Endian conversion routines.
40         // The Cortex-M3 inside the Cypress PSoC 5LP is a
41         // little-endian device.
42
43         bool isHostLE()
44         {
45                 union
46                 {
47                         int i;
48                         char c[sizeof(int)];
49                 } x;
50                 x.i = 1;
51                 return (x.c[0] == 1);
52         }
53
54         uint16_t toLE16(uint16_t in)
55         {
56                 if (isHostLE())
57                 {
58                         return in;
59                 }
60                 else
61                 {
62                         return (in >> 8) | (in << 8);
63                 }
64         }
65         uint16_t fromLE16(uint16_t in)
66         {
67                 return toLE16(in);
68         }
69
70         uint32_t toLE32(uint32_t in)
71         {
72                 if (isHostLE())
73                 {
74                         return in;
75                 }
76                 else
77                 {
78                         return (in >> 24) |
79                                 ((in >> 8) & 0xff00) |
80                                 ((in << 8) & 0xff0000) |
81                                 (in << 24);
82                 }
83         }
84         uint32_t fromLE32(uint32_t in)
85         {
86                 return toLE32(in);
87         }
88
89 }
90
91 S2S_BoardCfg
92 ConfigUtil::DefaultBoardConfig()
93 {
94         S2S_BoardCfg config;
95         memset(&config, 0, sizeof(config));
96
97         memcpy(config.magic, "BCFG", 4);
98
99
100         // Default to maximum fail-safe options.
101         config.flags6 = S2S_CFG_ENABLE_TERMINATOR;
102         config.selectionDelay = 255; // auto
103
104         return config;
105 }
106
107 S2S_TargetCfg
108 ConfigUtil::Default(size_t targetIdx)
109 {
110         S2S_TargetCfg config;
111         memset(&config, 0, sizeof(config));
112
113         config.scsiId = targetIdx;
114         if (targetIdx == 0)
115         {
116                 config.scsiId = config.scsiId | S2S_CFG_TARGET_ENABLED;
117         }
118         config.deviceType = S2S_CFG_FIXED;
119
120         // Default to maximum fail-safe options.
121         config.flagsDEPRECATED = 0;
122         config.deviceTypeModifier = 0;
123         config.sdSectorStart = 0;
124
125         // Default to 2GB. Many systems have trouble with > 2GB disks, and
126         // a few start to complain at 1GB.
127         config.scsiSectors = 4194303; // 2GB - 1 sector
128         config.bytesPerSector = 512;
129         config.sectorsPerTrack = 63;
130         config.headsPerCylinder = 255;
131         memcpy(config.vendor, " codesrc", 8);
132         memcpy(config.prodId, "         SCSI2SD", 16);
133         memcpy(config.revision, " 6.0", 4);
134         memcpy(config.serial, "1234567812345678", 16);
135
136         // Reserved fields, already set to 0
137         // config.reserved
138
139         // not supported yet.
140         // config.vpd
141
142         return config;
143 }
144
145
146 S2S_TargetCfg
147 ConfigUtil::fromBytes(const uint8_t* data)
148 {
149         S2S_TargetCfg result;
150         memcpy(&result, data, sizeof(S2S_TargetCfg));
151         result.sdSectorStart = toLE32(result.sdSectorStart);
152         result.scsiSectors = toLE32(result.scsiSectors);
153         result.bytesPerSector = toLE16(result.bytesPerSector);
154         result.sectorsPerTrack = toLE16(result.sectorsPerTrack);
155         result.headsPerCylinder = toLE16(result.headsPerCylinder);
156         return result;
157 }
158
159
160 std::vector<uint8_t>
161 ConfigUtil::toBytes(const S2S_TargetCfg& _config)
162 {
163         S2S_TargetCfg config(_config);
164         config.sdSectorStart = fromLE32(config.sdSectorStart);
165         config.scsiSectors = fromLE32(config.scsiSectors);
166         config.bytesPerSector = fromLE16(config.bytesPerSector);
167         config.sectorsPerTrack = fromLE16(config.sectorsPerTrack);
168         config.headsPerCylinder = fromLE16(config.headsPerCylinder);
169
170         const uint8_t* begin = reinterpret_cast<const uint8_t*>(&config);
171         return std::vector<uint8_t>(begin, begin + sizeof(config));
172 }
173
174 S2S_BoardCfg
175 ConfigUtil::boardConfigFromBytes(const uint8_t* data)
176 {
177         S2S_BoardCfg result;
178         memcpy(&result, data, sizeof(S2S_BoardCfg));
179
180         if (memcmp("BCFG", result.magic, 4))
181         {
182                 return DefaultBoardConfig();
183         }
184
185         return result;
186 }
187
188
189 std::vector<uint8_t>
190 ConfigUtil::boardConfigToBytes(const S2S_BoardCfg& _config)
191 {
192         S2S_BoardCfg config(_config);
193
194         memcpy(config.magic, "BCFG", 4);
195         const uint8_t* begin = reinterpret_cast<const uint8_t*>(&config);
196         return std::vector<uint8_t>(begin, begin + sizeof(config));
197 }
198
199 std::string
200 ConfigUtil::toXML(const S2S_TargetCfg& config)
201 {
202         std::stringstream s;
203
204         s <<
205                 "<SCSITarget id=\"" <<
206                         static_cast<int>(config.scsiId & S2S_CFG_TARGET_ID_BITS) << "\">\n" <<
207
208                 "       <enabled>" <<
209                         (config.scsiId & S2S_CFG_TARGET_ENABLED ? "true" : "false") <<
210                         "</enabled>\n" <<
211
212                 "\n" <<
213                 "       <!-- ********************************************************\n" <<
214                 "       Space separated list. Available options:\n" <<
215                 "       apple\t\tReturns Apple-specific mode pages\n" <<
216                 "       omti\t\tOMTI host non-standard link control\n" <<
217                 "       ********************************************************* -->\n" <<
218                 "       <quirks>";
219         if (config.quirks == S2S_CFG_QUIRKS_APPLE)
220         {
221                 s << "apple";
222         }
223         else if (config.quirks == S2S_CFG_QUIRKS_OMTI)
224         {
225                 s << "omti";
226         }
227
228         s <<
229                         "</quirks>\n" <<
230
231                 "\n\n" <<
232                 "       <!-- ********************************************************\n" <<
233                 "       0x0    Fixed hard drive.\n" <<
234                 "       0x1    Removable drive.\n" <<
235                 "       0x2    Optical drive  (ie. CD drive).\n" <<
236                 "       0x3    1.44MB Floppy Drive.\n" <<
237                 "       ********************************************************* -->\n" <<
238                 "       <deviceType>0x" <<
239                                 std::hex << static_cast<int>(config.deviceType) <<
240                         "</deviceType>\n" <<
241
242                 "\n\n" <<
243                 "       <!-- ********************************************************\n" <<
244                 "       Device type modifier is usually 0x00. Only change this if your\n" <<
245                 "       OS requires some special value.\n" <<
246                 "\n" <<
247                 "       0x4C    Data General Micropolis disk\n" <<
248                 "       ********************************************************* -->\n" <<
249                 "       <deviceTypeModifier>0x" <<
250                                 std::hex << static_cast<int>(config.deviceTypeModifier) <<
251                         "</deviceTypeModifier>\n" <<
252
253                 "\n\n" <<
254                 "       <!-- ********************************************************\n" <<
255                 "       SD card offset, as a sector number (always 512 bytes).\n" <<
256                 "       ********************************************************* -->\n" <<
257                 "       <sdSectorStart>" << std::dec << config.sdSectorStart << "</sdSectorStart>\n" <<
258                 "\n\n" <<
259                 "       <!-- ********************************************************\n" <<
260                 "       Drive geometry settings.\n" <<
261                 "       ********************************************************* -->\n" <<
262                 "\n"
263                 "       <scsiSectors>" << std::dec << config.scsiSectors << "</scsiSectors>\n" <<
264                 "       <bytesPerSector>" << std::dec << config.bytesPerSector << "</bytesPerSector>\n" <<
265                 "       <sectorsPerTrack>" << std::dec << config.sectorsPerTrack<< "</sectorsPerTrack>\n" <<
266                 "       <headsPerCylinder>" << std::dec << config.headsPerCylinder << "</headsPerCylinder>\n" <<
267                 "\n\n" <<
268                 "       <!-- ********************************************************\n" <<
269                 "       Drive identification information. The SCSI2SD doesn't\n" <<
270                 "       care what these are set to. Use these strings to trick a OS\n" <<
271                 "       thinking a specific hard drive model is attached.\n" <<
272                 "       ********************************************************* -->\n" <<
273                 "\n"
274                 "       <!-- 8 character vendor string -->\n" <<
275                 "       <!-- For Apple HD SC Setup/Drive Setup, use ' SEAGATE' -->\n" <<
276                 "       <vendor>" << std::string(config.vendor, 8) << "</vendor>\n" <<
277                 "\n" <<
278                 "       <!-- 16 character produce identifier -->\n" <<
279                 "       <!-- For Apple HD SC Setup/Drive Setup, use '          ST225N' -->\n" <<
280                 "       <prodId>" << std::string(config.prodId, 16) << "</prodId>\n" <<
281                 "\n" <<
282                 "       <!-- 4 character product revision number -->\n" <<
283                 "       <!-- For Apple HD SC Setup/Drive Setup, use '1.0 ' -->\n" <<
284                 "       <revision>" << std::string(config.revision, 4) << "</revision>\n" <<
285                 "\n" <<
286                 "       <!-- 16 character serial number -->\n" <<
287                 "       <serial>" << std::string(config.serial, 16) << "</serial>\n" <<
288                 "\n" <<
289                 "</SCSITarget>\n";
290
291         return s.str();
292 }
293
294 std::string
295 ConfigUtil::toXML(const S2S_BoardCfg& config)
296 {
297         std::stringstream s;
298
299         s << "<S2S_BoardCfg>\n" <<
300
301                 "       <!-- ********************************************************\n" <<
302                 "       Enable the onboard active terminator.\n"
303                 "       Both ends of the SCSI chain should be terminated. Disable\n" <<
304                 "       only if the SCSI2SD is in the middle of a chain with other\n" <<
305                 "       devices.\n" <<
306                 "       ********************************************************* -->\n" <<
307                 "       <enableTerminator>" <<
308                         (config.flags6 & S2S_CFG_ENABLE_TERMINATOR ? "true" : "false") <<
309                         "</enableTerminator>\n" <<
310
311                 "       <unitAttention>" <<
312                         (config.flags & S2S_CFG_ENABLE_UNIT_ATTENTION ? "true" : "false") <<
313                         "</unitAttention>\n" <<
314
315                 "       <parity>" <<
316                         (config.flags & S2S_CFG_ENABLE_PARITY ? "true" : "false") <<
317                         "</parity>\n" <<
318
319                 "       <!-- ********************************************************\n" <<
320                 "       Only set to true when using with a fast SCSI2 host\n " <<
321                 "       controller. This can cause problems with older/slower\n" <<
322                 "       hardware.\n" <<
323                 "       ********************************************************* -->\n" <<
324                 "       <enableScsi2>" <<
325                         (config.flags & S2S_CFG_ENABLE_SCSI2 ? "true" : "false") <<
326                         "</enableScsi2>\n" <<
327
328                 "       <!-- ********************************************************\n" <<
329                 "       Respond to very short duration selection attempts. This supports\n" <<
330                 "       non-standard hardware, but is generally safe to enable.\n" <<
331                 "       Required for Philips P2000C.\n" <<
332                 "       ********************************************************* -->\n" <<
333                 "       <selLatch>" <<
334                         (config.flags & S2S_CFG_ENABLE_SEL_LATCH? "true" : "false") <<
335                         "</selLatch>\n" <<
336
337
338                 "       <!-- ********************************************************\n" <<
339                 "       Convert luns to IDs. The unit must already be configured to respond\n" <<
340                 "       on the ID. Allows dual drives to be accessed from a \n" <<
341                 "       XEBEC S1410 SASI bridge.\n" <<
342                 "       eg. Configured for dual drives as IDs 0 and 1, but the XEBEC will\n" <<
343                 "       access the second disk as ID0, lun 1.\n" <<
344                 "       See ttp://bitsavers.trailing-edge.com/pdf/xebec/104524C_S1410Man_Aug83.pdf\n" <<
345                 "       ********************************************************* -->\n" <<
346                 "       <mapLunsToIds>" <<
347                         (config.flags & S2S_CFG_MAP_LUNS_TO_IDS ? "true" : "false") <<
348                         "</mapLunsToIds>\n" <<
349
350
351                 "       <!-- ********************************************************\n" <<
352                 "       Delay (in milliseconds) before responding to a SCSI selection.\n" <<
353                 "       255 (auto) sets it to 0 for SCSI2 hosts and 1ms otherwise.\n" <<
354                 "       Some samplers need this set to 1 manually.\n" <<
355                 "       ********************************************************* -->\n" <<
356                 "       <selectionDelay>" << static_cast<int>(config.selectionDelay) << "</selectionDelay>\n" <<
357
358                 "       <!-- ********************************************************\n" <<
359                 "       Startup delay (in seconds) before responding to the SCSI bus \n" <<
360                 "       after power on. Default = 0.\n" <<
361                 "       ********************************************************* -->\n" <<
362                 "       <startupDelay>" << static_cast<int>(config.startupDelay) << "</startupDelay>\n" <<
363
364                 "       <!-- ********************************************************\n" <<
365                 "       Speed limit the SCSI interface. This is the -max- speed the \n" <<
366                 "       device will run at. The actual spee depends on the capability\n" <<
367                 "       of the host controller.\n" <<
368                 "       0       No limit\n" <<
369                 "       1       Async 1.5MB/s\n" <<
370                 "       2       Async 3.3MB/s\n" <<
371                 "       3       Async 5MB/s\n" <<
372                 "       4       Sync 5MB/s\n" <<
373                 "       5       Sync 10MB/s\n" <<
374                 "       ********************************************************* -->\n" <<
375                 "       <scsiSpeed>" << static_cast<int>(config.scsiSpeed) << "</scsiSpeed>\n" <<
376
377                 "</S2S_BoardCfg>\n";
378
379         return s.str();
380 }
381
382
383 static uint64_t parseInt(wxXmlNode* node, uint64_t limit)
384 {
385         std::string str(node->GetNodeContent().mb_str());
386         if (str.empty())
387         {
388                 throw std::runtime_error("Empty " + node->GetName());
389         }
390
391         std::stringstream s;
392         if (str.find("0x") == 0)
393         {
394                 s << std::hex << str.substr(2);
395         }
396         else
397         {
398                 s << str;
399         }
400
401         uint64_t result;
402         s >> result;
403         if (!s)
404         {
405                 throw std::runtime_error("Invalid value for " + node->GetName());
406         }
407
408         if (result > limit)
409         {
410                 std::stringstream msg;
411                 msg << "Invalid value for " << node->GetName() <<
412                         " (max=" << limit << ")";
413                 throw std::runtime_error(msg.str());
414         }
415         return result;
416 }
417
418 static S2S_TargetCfg
419 parseTarget(wxXmlNode* node)
420 {
421         int id;
422         {
423                 std::stringstream s;
424                 s << node->GetAttribute("id", "7");
425                 s >> id;
426                 if (!s) throw std::runtime_error("Could not parse SCSITarget id attr");
427         }
428         S2S_TargetCfg result = ConfigUtil::Default(id & 0x7);
429
430         wxXmlNode *child = node->GetChildren();
431         while (child)
432         {
433                 if (child->GetName() == "enabled")
434                 {
435                         std::string s(child->GetNodeContent().mb_str());
436                         if (s == "true")
437                         {
438                                 result.scsiId |= S2S_CFG_TARGET_ENABLED;
439                         }
440                         else
441                         {
442                                 result.scsiId = result.scsiId & ~S2S_CFG_TARGET_ENABLED;
443                         }
444                 }
445                 else if (child->GetName() == "quirks")
446                 {
447                         std::stringstream s(std::string(child->GetNodeContent().mb_str()));
448                         std::string quirk;
449                         while (s >> quirk)
450                         {
451                                 if (quirk == "apple")
452                                 {
453                                         result.quirks |= S2S_CFG_QUIRKS_APPLE;
454                                 }
455                                 else if (quirk == "omti")
456                                 {
457                                         result.quirks |= S2S_CFG_QUIRKS_OMTI;
458                                 }
459                         }
460                 }
461                 else if (child->GetName() == "deviceType")
462                 {
463                         result.deviceType = parseInt(child, 0xFF);
464                 }
465                 else if (child->GetName() == "deviceTypeModifier")
466                 {
467                         result.deviceTypeModifier = parseInt(child, 0xFF);
468                 }
469                 else if (child->GetName() == "sdSectorStart")
470                 {
471                         result.sdSectorStart = parseInt(child, 0xFFFFFFFF);
472                 }
473                 else if (child->GetName() == "scsiSectors")
474                 {
475                         result.scsiSectors = parseInt(child, 0xFFFFFFFF);
476                 }
477                 else if (child->GetName() == "bytesPerSector")
478                 {
479                         result.bytesPerSector = parseInt(child, 8192);
480                 }
481                 else if (child->GetName() == "sectorsPerTrack")
482                 {
483                         result.sectorsPerTrack = parseInt(child, 255);
484                 }
485                 else if (child->GetName() == "headsPerCylinder")
486                 {
487                         result.headsPerCylinder = parseInt(child, 255);
488                 }
489                 else if (child->GetName() == "vendor")
490                 {
491                         std::string s(child->GetNodeContent().mb_str());
492                         s = s.substr(0, sizeof(result.vendor));
493                         memset(result.vendor, ' ', sizeof(result.vendor));
494                         memcpy(result.vendor, s.c_str(), s.size());
495                 }
496                 else if (child->GetName() == "prodId")
497                 {
498                         std::string s(child->GetNodeContent().mb_str());
499                         s = s.substr(0, sizeof(result.prodId));
500                         memset(result.prodId, ' ', sizeof(result.prodId));
501                         memcpy(result.prodId, s.c_str(), s.size());
502                 }
503                 else if (child->GetName() == "revision")
504                 {
505                         std::string s(child->GetNodeContent().mb_str());
506                         s = s.substr(0, sizeof(result.revision));
507                         memset(result.revision, ' ', sizeof(result.revision));
508                         memcpy(result.revision, s.c_str(), s.size());
509                 }
510                 else if (child->GetName() == "serial")
511                 {
512                         std::string s(child->GetNodeContent().mb_str());
513                         s = s.substr(0, sizeof(result.serial));
514                         memset(result.serial, ' ', sizeof(result.serial));
515                         memcpy(result.serial, s.c_str(), s.size());
516                 }
517
518
519                 child = child->GetNext();
520         }
521         return result;
522 }
523
524 static S2S_BoardCfg
525 parseBoardConfig(wxXmlNode* node)
526 {
527         S2S_BoardCfg result = ConfigUtil::DefaultBoardConfig();
528
529         wxXmlNode *child = node->GetChildren();
530         while (child)
531         {
532                 if (child->GetName() == "selectionDelay")
533                 {
534                         result.selectionDelay = parseInt(child, 255);
535                 }
536                 else if (child->GetName() == "startupDelay")
537                 {
538                         result.startupDelay = parseInt(child, 255);
539                 }
540                 else if (child->GetName() == "unitAttention")
541                 {
542                         std::string s(child->GetNodeContent().mb_str());
543                         if (s == "true")
544                         {
545                                 result.flags |= S2S_CFG_ENABLE_UNIT_ATTENTION;
546                         }
547                         else
548                         {
549                                 result.flags = result.flags & ~S2S_CFG_ENABLE_UNIT_ATTENTION;
550                         }
551                 }
552                 else if (child->GetName() == "parity")
553                 {
554                         std::string s(child->GetNodeContent().mb_str());
555                         if (s == "true")
556                         {
557                                 result.flags |= S2S_CFG_ENABLE_PARITY;
558                         }
559                         else
560                         {
561                                 result.flags = result.flags & ~S2S_CFG_ENABLE_PARITY;
562                         }
563                 }
564                 else if (child->GetName() == "enableScsi2")
565                 {
566                         std::string s(child->GetNodeContent().mb_str());
567                         if (s == "true")
568                         {
569                                 result.flags |= S2S_CFG_ENABLE_SCSI2;
570                         }
571                         else
572                         {
573                                 result.flags = result.flags & ~S2S_CFG_ENABLE_SCSI2;
574                         }
575                 }
576                 else if (child->GetName() == "enableTerminator")
577                 {
578                         std::string s(child->GetNodeContent().mb_str());
579                         if (s == "true")
580                         {
581                                 result.flags6 |= S2S_CFG_ENABLE_TERMINATOR;
582                         }
583                         else
584                         {
585                                 result.flags6 = result.flags & ~S2S_CFG_ENABLE_TERMINATOR;
586                         }
587                 }
588                 else if (child->GetName() == "selLatch")
589                 {
590                         std::string s(child->GetNodeContent().mb_str());
591                         if (s == "true")
592                         {
593                                 result.flags |= S2S_CFG_ENABLE_SEL_LATCH;
594                         }
595                         else
596                         {
597                                 result.flags = result.flags & ~S2S_CFG_ENABLE_SEL_LATCH;
598                         }
599                 }
600                 else if (child->GetName() == "mapLunsToIds")
601                 {
602                         std::string s(child->GetNodeContent().mb_str());
603                         if (s == "true")
604                         {
605                                 result.flags |= S2S_CFG_MAP_LUNS_TO_IDS;
606                         }
607                         else
608                         {
609                                 result.flags = result.flags & ~S2S_CFG_MAP_LUNS_TO_IDS;
610                         }
611                 }
612                 else if (child->GetName() == "scsiSpeed")
613                 {
614                         result.scsiSpeed = parseInt(child, S2S_CFG_SPEED_SYNC_10);
615                 }
616                 child = child->GetNext();
617         }
618         return result;
619 }
620
621
622 std::pair<S2S_BoardCfg, std::vector<S2S_TargetCfg>>
623 ConfigUtil::fromXML(const std::string& filename)
624 {
625         wxXmlDocument doc;
626         if (!doc.Load(filename))
627         {
628                 throw std::runtime_error("Could not load XML file");
629         }
630
631         // start processing the XML file
632         if (doc.GetRoot()->GetName() != "SCSI2SD")
633         {
634                 throw std::runtime_error("Invalid root node, expected <SCSI2SD>");
635         }
636
637         S2S_BoardCfg boardConfig = DefaultBoardConfig();
638         int boardConfigFound = 0;
639
640         std::vector<S2S_TargetCfg> targets;
641         wxXmlNode *child = doc.GetRoot()->GetChildren();
642         while (child)
643         {
644                 if (child->GetName() == "SCSITarget")
645                 {
646                         targets.push_back(parseTarget(child));
647                 }
648                 else if (child->GetName() == "S2S_BoardCfg")
649                 {
650                         boardConfig = parseBoardConfig(child);
651                         boardConfigFound = 1;
652                 }
653                 child = child->GetNext();
654         }
655
656         if (!boardConfigFound && targets.size() > 0)
657         {
658                 boardConfig.flags = targets[0].flagsDEPRECATED;
659         }
660         return make_pair(boardConfig, targets);
661 }
662