9db2ded62cdd97b279096d3974fbebb41d8ca5fe
[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                 "       ********************************************************* -->\n" <<
217                 "       <quirks>" <<
218                         (config.quirks & S2S_CFG_QUIRKS_APPLE ? "apple" : "") <<
219                         "</quirks>\n" <<
220
221                 "\n\n" <<
222                 "       <!-- ********************************************************\n" <<
223                 "       0x0    Fixed hard drive.\n" <<
224                 "       0x1    Removable drive.\n" <<
225                 "       0x2    Optical drive  (ie. CD drive).\n" <<
226                 "       0x3    1.44MB Floppy Drive.\n" <<
227                 "       ********************************************************* -->\n" <<
228                 "       <deviceType>0x" <<
229                                 std::hex << static_cast<int>(config.deviceType) <<
230                         "</deviceType>\n" <<
231
232                 "\n\n" <<
233                 "       <!-- ********************************************************\n" <<
234                 "       Device type modifier is usually 0x00. Only change this if your\n" <<
235                 "       OS requires some special value.\n" <<
236                 "\n" <<
237                 "       0x4C    Data General Micropolis disk\n" <<
238                 "       ********************************************************* -->\n" <<
239                 "       <deviceTypeModifier>0x" <<
240                                 std::hex << static_cast<int>(config.deviceTypeModifier) <<
241                         "</deviceTypeModifier>\n" <<
242
243                 "\n\n" <<
244                 "       <!-- ********************************************************\n" <<
245                 "       SD card offset, as a sector number (always 512 bytes).\n" <<
246                 "       ********************************************************* -->\n" <<
247                 "       <sdSectorStart>" << std::dec << config.sdSectorStart << "</sdSectorStart>\n" <<
248                 "\n\n" <<
249                 "       <!-- ********************************************************\n" <<
250                 "       Drive geometry settings.\n" <<
251                 "       ********************************************************* -->\n" <<
252                 "\n"
253                 "       <scsiSectors>" << std::dec << config.scsiSectors << "</scsiSectors>\n" <<
254                 "       <bytesPerSector>" << std::dec << config.bytesPerSector << "</bytesPerSector>\n" <<
255                 "       <sectorsPerTrack>" << std::dec << config.sectorsPerTrack<< "</sectorsPerTrack>\n" <<
256                 "       <headsPerCylinder>" << std::dec << config.headsPerCylinder << "</headsPerCylinder>\n" <<
257                 "\n\n" <<
258                 "       <!-- ********************************************************\n" <<
259                 "       Drive identification information. The SCSI2SD doesn't\n" <<
260                 "       care what these are set to. Use these strings to trick a OS\n" <<
261                 "       thinking a specific hard drive model is attached.\n" <<
262                 "       ********************************************************* -->\n" <<
263                 "\n"
264                 "       <!-- 8 character vendor string -->\n" <<
265                 "       <!-- For Apple HD SC Setup/Drive Setup, use ' SEAGATE' -->\n" <<
266                 "       <vendor>" << std::string(config.vendor, 8) << "</vendor>\n" <<
267                 "\n" <<
268                 "       <!-- 16 character produce identifier -->\n" <<
269                 "       <!-- For Apple HD SC Setup/Drive Setup, use '          ST225N' -->\n" <<
270                 "       <prodId>" << std::string(config.prodId, 16) << "</prodId>\n" <<
271                 "\n" <<
272                 "       <!-- 4 character product revision number -->\n" <<
273                 "       <!-- For Apple HD SC Setup/Drive Setup, use '1.0 ' -->\n" <<
274                 "       <revision>" << std::string(config.revision, 4) << "</revision>\n" <<
275                 "\n" <<
276                 "       <!-- 16 character serial number -->\n" <<
277                 "       <serial>" << std::string(config.serial, 16) << "</serial>\n" <<
278                 "\n" <<
279                 "</SCSITarget>\n";
280
281         return s.str();
282 }
283
284 std::string
285 ConfigUtil::toXML(const S2S_BoardCfg& config)
286 {
287         std::stringstream s;
288
289         s << "<S2S_BoardCfg>\n" <<
290
291                 "       <!-- ********************************************************\n" <<
292                 "       Enable the onboard active terminator.\n"
293                 "       Both ends of the SCSI chain should be terminated. Disable\n" <<
294                 "       only if the SCSI2SD is in the middle of a chain with other\n" <<
295                 "       devices.\n" <<
296                 "       ********************************************************* -->\n" <<
297                 "       <enableTerminator>" <<
298                         (config.flags6 & S2S_CFG_ENABLE_TERMINATOR ? "true" : "false") <<
299                         "</enableTerminator>\n" <<
300
301                 "       <unitAttention>" <<
302                         (config.flags & S2S_CFG_ENABLE_UNIT_ATTENTION ? "true" : "false") <<
303                         "</unitAttention>\n" <<
304
305                 "       <parity>" <<
306                         (config.flags & S2S_CFG_ENABLE_PARITY ? "true" : "false") <<
307                         "</parity>\n" <<
308
309                 "       <!-- ********************************************************\n" <<
310                 "       Only set to true when using with a fast SCSI2 host\n " <<
311                 "       controller. This can cause problems with older/slower\n" <<
312                 "       hardware.\n" <<
313                 "       ********************************************************* -->\n" <<
314                 "       <enableScsi2>" <<
315                         (config.flags & S2S_CFG_ENABLE_SCSI2 ? "true" : "false") <<
316                         "</enableScsi2>\n" <<
317
318                 "       <enableCache>" <<
319                         (config.flags & S2S_CFG_ENABLE_CACHE ? "true" : "false") <<
320                         "</enableCache>\n" <<
321
322                 "       <!-- ********************************************************\n" <<
323                 "       Setting to 'true' will result in increased performance at the\n" <<
324                 "       cost of lower noise immunity.\n" <<
325                 "       Only set to true when using short cables with only 1 or two\n" <<
326                 "       devices. This should remain off when using external SCSI1 DB25\n" <<
327                 "       cables.\n" <<
328                 "       ********************************************************* -->\n" <<
329                 "       <disableGlitchFilter>" <<
330                         (config.flags & S2S_CFG_DISABLE_GLITCH ? "true" : "false") <<
331                         "</disableGlitchFilter>\n" <<
332
333
334                 "       <enableDisconnect>" <<
335                         (config.flags & S2S_CFG_ENABLE_DISCONNECT ? "true" : "false") <<
336                         "</enableDisconnect>\n" <<
337
338                 "       <!-- ********************************************************\n" <<
339                 "       Respond to very short duration selection attempts. This supports\n" <<
340                 "       non-standard hardware, but is generally safe to enable.\n" <<
341                 "       Required for Philips P2000C.\n" <<
342                 "       ********************************************************* -->\n" <<
343                 "       <selLatch>" <<
344                         (config.flags & S2S_CFG_ENABLE_SEL_LATCH? "true" : "false") <<
345                         "</selLatch>\n" <<
346
347
348                 "       <!-- ********************************************************\n" <<
349                 "       Convert luns to IDs. The unit must already be configured to respond\n" <<
350                 "       on the ID. Allows dual drives to be accessed from a \n" <<
351                 "       XEBEC S1410 SASI bridge.\n" <<
352                 "       eg. Configured for dual drives as IDs 0 and 1, but the XEBEC will\n" <<
353                 "       access the second disk as ID0, lun 1.\n" <<
354                 "       See ttp://bitsavers.trailing-edge.com/pdf/xebec/104524C_S1410Man_Aug83.pdf\n" <<
355                 "       ********************************************************* -->\n" <<
356                 "       <mapLunsToIds>" <<
357                         (config.flags & S2S_CFG_MAP_LUNS_TO_IDS ? "true" : "false") <<
358                         "</mapLunsToIds>\n" <<
359                 "</S2S_BoardCfg>\n";
360
361         return s.str();
362 }
363
364
365 static uint64_t parseInt(wxXmlNode* node, uint64_t limit)
366 {
367         std::string str(node->GetNodeContent().mb_str());
368         if (str.empty())
369         {
370                 throw std::runtime_error("Empty " + node->GetName());
371         }
372
373         std::stringstream s;
374         if (str.find("0x") == 0)
375         {
376                 s << std::hex << str.substr(2);
377         }
378         else
379         {
380                 s << str;
381         }
382
383         uint64_t result;
384         s >> result;
385         if (!s)
386         {
387                 throw std::runtime_error("Invalid value for " + node->GetName());
388         }
389
390         if (result > limit)
391         {
392                 std::stringstream msg;
393                 msg << "Invalid value for " << node->GetName() <<
394                         " (max=" << limit << ")";
395                 throw std::runtime_error(msg.str());
396         }
397         return result;
398 }
399
400 static S2S_TargetCfg
401 parseTarget(wxXmlNode* node)
402 {
403         int id;
404         {
405                 std::stringstream s;
406                 s << node->GetAttribute("id", "7");
407                 s >> id;
408                 if (!s) throw std::runtime_error("Could not parse SCSITarget id attr");
409         }
410         S2S_TargetCfg result = ConfigUtil::Default(id & 0x7);
411
412         wxXmlNode *child = node->GetChildren();
413         while (child)
414         {
415                 if (child->GetName() == "enabled")
416                 {
417                         std::string s(child->GetNodeContent().mb_str());
418                         if (s == "true")
419                         {
420                                 result.scsiId |= S2S_CFG_TARGET_ENABLED;
421                         }
422                         else
423                         {
424                                 result.scsiId = result.scsiId & ~S2S_CFG_TARGET_ENABLED;
425                         }
426                 }
427                 else if (child->GetName() == "quirks")
428                 {
429                         std::stringstream s(std::string(child->GetNodeContent().mb_str()));
430                         std::string quirk;
431                         while (s >> quirk)
432                         {
433                                 if (quirk == "apple")
434                                 {
435                                         result.quirks |= S2S_CFG_QUIRKS_APPLE;
436                                 }
437                         }
438                 }
439                 else if (child->GetName() == "deviceType")
440                 {
441                         result.deviceType = parseInt(child, 0xFF);
442                 }
443                 else if (child->GetName() == "deviceTypeModifier")
444                 {
445                         result.deviceTypeModifier = parseInt(child, 0xFF);
446                 }
447                 else if (child->GetName() == "sdSectorStart")
448                 {
449                         result.sdSectorStart = parseInt(child, 0xFFFFFFFF);
450                 }
451                 else if (child->GetName() == "scsiSectors")
452                 {
453                         result.scsiSectors = parseInt(child, 0xFFFFFFFF);
454                 }
455                 else if (child->GetName() == "bytesPerSector")
456                 {
457                         result.bytesPerSector = parseInt(child, 8192);
458                 }
459                 else if (child->GetName() == "sectorsPerTrack")
460                 {
461                         result.sectorsPerTrack = parseInt(child, 255);
462                 }
463                 else if (child->GetName() == "headsPerCylinder")
464                 {
465                         result.headsPerCylinder = parseInt(child, 255);
466                 }
467                 else if (child->GetName() == "vendor")
468                 {
469                         std::string s(child->GetNodeContent().mb_str());
470                         s = s.substr(0, sizeof(result.vendor));
471                         memset(result.vendor, ' ', sizeof(result.vendor));
472                         memcpy(result.vendor, s.c_str(), s.size());
473                 }
474                 else if (child->GetName() == "prodId")
475                 {
476                         std::string s(child->GetNodeContent().mb_str());
477                         s = s.substr(0, sizeof(result.prodId));
478                         memset(result.prodId, ' ', sizeof(result.prodId));
479                         memcpy(result.prodId, s.c_str(), s.size());
480                 }
481                 else if (child->GetName() == "revision")
482                 {
483                         std::string s(child->GetNodeContent().mb_str());
484                         s = s.substr(0, sizeof(result.revision));
485                         memset(result.revision, ' ', sizeof(result.revision));
486                         memcpy(result.revision, s.c_str(), s.size());
487                 }
488                 else if (child->GetName() == "serial")
489                 {
490                         std::string s(child->GetNodeContent().mb_str());
491                         s = s.substr(0, sizeof(result.serial));
492                         memset(result.serial, ' ', sizeof(result.serial));
493                         memcpy(result.serial, s.c_str(), s.size());
494                 }
495
496
497                 child = child->GetNext();
498         }
499         return result;
500 }
501
502 static S2S_BoardCfg
503 parseBoardConfig(wxXmlNode* node)
504 {
505         S2S_BoardCfg result = ConfigUtil::DefaultBoardConfig();
506
507         wxXmlNode *child = node->GetChildren();
508         while (child)
509         {
510 // FIXME WHERE IS SELECTION DELAY ? STARTUP DELAY ? FFS.
511                 if (child->GetName() == "unitAttention")
512                 {
513                         std::string s(child->GetNodeContent().mb_str());
514                         if (s == "true")
515                         {
516                                 result.flags |= S2S_CFG_ENABLE_UNIT_ATTENTION;
517                         }
518                         else
519                         {
520                                 result.flags = result.flags & ~S2S_CFG_ENABLE_UNIT_ATTENTION;
521                         }
522                 }
523                 else if (child->GetName() == "parity")
524                 {
525                         std::string s(child->GetNodeContent().mb_str());
526                         if (s == "true")
527                         {
528                                 result.flags |= S2S_CFG_ENABLE_PARITY;
529                         }
530                         else
531                         {
532                                 result.flags = result.flags & ~S2S_CFG_ENABLE_PARITY;
533                         }
534                 }
535                 else if (child->GetName() == "enableScsi2")
536                 {
537                         std::string s(child->GetNodeContent().mb_str());
538                         if (s == "true")
539                         {
540                                 result.flags |= S2S_CFG_ENABLE_SCSI2;
541                         }
542                         else
543                         {
544                                 result.flags = result.flags & ~S2S_CFG_ENABLE_SCSI2;
545                         }
546                 }
547                 else if (child->GetName() == "disableGlitchFilter")
548                 {
549                         std::string s(child->GetNodeContent().mb_str());
550                         if (s == "true")
551                         {
552                                 result.flags |= S2S_CFG_DISABLE_GLITCH;
553                         }
554                         else
555                         {
556                                 result.flags = result.flags & ~S2S_CFG_DISABLE_GLITCH;
557                         }
558                 }
559                 else if (child->GetName() == "enableTerminator")
560                 {
561                         std::string s(child->GetNodeContent().mb_str());
562                         if (s == "true")
563                         {
564                                 result.flags6 |= S2S_CFG_ENABLE_TERMINATOR;
565                         }
566                         else
567                         {
568                                 result.flags6 = result.flags & ~S2S_CFG_ENABLE_TERMINATOR;
569                         }
570                 }
571                 else if (child->GetName() == "enableCache")
572                 {
573                         std::string s(child->GetNodeContent().mb_str());
574                         if (s == "true")
575                         {
576                                 result.flags |= S2S_CFG_ENABLE_CACHE;
577                         }
578                         else
579                         {
580                                 result.flags = result.flags & ~S2S_CFG_ENABLE_CACHE;
581                         }
582                 }
583                 else if (child->GetName() == "enableDisconnect")
584                 {
585                         std::string s(child->GetNodeContent().mb_str());
586                         if (s == "true")
587                         {
588                                 result.flags |= S2S_CFG_ENABLE_DISCONNECT;
589                         }
590                         else
591                         {
592                                 result.flags = result.flags & ~S2S_CFG_ENABLE_DISCONNECT;
593                         }
594                 }
595                 else if (child->GetName() == "selLatch")
596                 {
597                         std::string s(child->GetNodeContent().mb_str());
598                         if (s == "true")
599                         {
600                                 result.flags |= S2S_CFG_ENABLE_SEL_LATCH;
601                         }
602                         else
603                         {
604                                 result.flags = result.flags & ~S2S_CFG_ENABLE_SEL_LATCH;
605                         }
606                 }
607                 else if (child->GetName() == "mapLunsToIds")
608                 {
609                         std::string s(child->GetNodeContent().mb_str());
610                         if (s == "true")
611                         {
612                                 result.flags |= S2S_CFG_MAP_LUNS_TO_IDS;
613                         }
614                         else
615                         {
616                                 result.flags = result.flags & ~S2S_CFG_MAP_LUNS_TO_IDS;
617                         }
618                 }
619                 child = child->GetNext();
620         }
621         return result;
622 }
623
624
625 std::pair<S2S_BoardCfg, std::vector<S2S_TargetCfg>>
626 ConfigUtil::fromXML(const std::string& filename)
627 {
628         wxXmlDocument doc;
629         if (!doc.Load(filename))
630         {
631                 throw std::runtime_error("Could not load XML file");
632         }
633
634         // start processing the XML file
635         if (doc.GetRoot()->GetName() != "SCSI2SD")
636         {
637                 throw std::runtime_error("Invalid root node, expected <SCSI2SD>");
638         }
639
640         S2S_BoardCfg boardConfig = DefaultBoardConfig();
641         int boardConfigFound = 0;
642
643         std::vector<S2S_TargetCfg> targets;
644         wxXmlNode *child = doc.GetRoot()->GetChildren();
645         while (child)
646         {
647                 if (child->GetName() == "SCSITarget")
648                 {
649                         targets.push_back(parseTarget(child));
650                 }
651                 else if (child->GetName() == "S2S_BoardCfg")
652                 {
653                         boardConfig = parseBoardConfig(child);
654                         boardConfigFound = 1;
655                 }
656                 child = child->GetNext();
657         }
658
659         if (!boardConfigFound && targets.size() > 0)
660         {
661                 boardConfig.flags = targets[0].flagsDEPRECATED;
662         }
663         return make_pair(boardConfig, targets);
664 }
665