QuirksCheck.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /**
  2. * Copyright (C) 2023 Eric Helgeson
  3. * Portions ZuluSCSI™ - Copyright (c) 2023 Rabbit Hole Computing™
  4. *
  5. * This file is licensed under the GPL version 3 or any later version. 
  6. * It is derived from BlueSCSI_platform_config_hook.cpp in BluSCSI-v2
  7. *
  8. * https://www.gnu.org/licenses/gpl-3.0.html
  9. * ----
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version. 
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18. * GNU General Public License for more details. 
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  22. **/
  23. #include <minIni.h>
  24. #include "ZuluSCSI_disk.h"
  25. #include "ZuluSCSI_log.h"
  26. #include "ZuluSCSI_settings.h"
  27. #include "QuirksCheck.h"
  28. #include <assert.h>
  29. #include <stdint.h>
  30. static bool isValidMacintoshImage(image_config_t *img)
  31. {
  32. bool result = true;
  33. const char apple_magic[2] = {0x45, 0x52};
  34. const char block_size[2] = {0x02, 0x00}; // 512 BE == 2
  35. const char lido_sig[4] = {'C', 'M', 'S', '_' };
  36. uint8_t tmp[SD_SECTOR_SIZE];
  37. // Check for Apple Magic
  38. img->file.seek(0);
  39. img->file.read(tmp, SD_SECTOR_SIZE);
  40. if (memcmp(apple_magic, tmp, 2) != 0)
  41. {
  42. dbgmsg("---- Apple magic not found.");
  43. result = false;
  44. }
  45. // Check HFS Block size is 512
  46. if (memcmp(block_size, &tmp[2], 2) != 0)
  47. {
  48. dbgmsg("---- Block size not 512", block_size);
  49. result = false;
  50. }
  51. uint8_t *mac_driver = &tmp[MACINTOSH_SCSI_DRIVER_OFFSET];
  52. uint32_t driver_offset_blocks = mac_driver[0] << 24 |
  53. mac_driver[1] << 16 |
  54. mac_driver[2] << 8 |
  55. mac_driver[3];
  56. // Find size of SCSI Driver partition
  57. uint8_t *mac_driver_size = &tmp[MACINTOSH_SCSI_DRIVER_SIZE_OFFSET];
  58. uint32_t driver_size_blocks = mac_driver_size[0] << 8 | mac_driver_size[1];
  59. // SCSI Driver sanity checks
  60. if((driver_size_blocks * MACINTOSH_BLOCK_SIZE) > MACINTOSH_SCSI_DRIVER_MAX_SIZE ||
  61. (driver_offset_blocks * MACINTOSH_BLOCK_SIZE) > img->file.size())
  62. {
  63. dbgmsg("---- Invalid Macintosh SCSI Driver partition detected.");
  64. result = false;
  65. }
  66. // Contains Lido Driver - driver causes issues on a Mac Plus and is generally slower than the Apple 4.3 or FWB.
  67. // Also causes compatibility issues with other drivers.
  68. // Mac block sizes should be the same size SD sector sizes for raw seeks, and reads to work
  69. assert(MACINTOSH_BLOCK_SIZE == SD_SECTOR_SIZE);
  70. img->file.seek(driver_offset_blocks * MACINTOSH_BLOCK_SIZE);
  71. img->file.read(tmp, SD_SECTOR_SIZE);
  72. uint8_t* lido_driver = &tmp[LIDO_SIG_OFFSET];
  73. if(memcmp(lido_sig, lido_driver, 4) == 0)
  74. {
  75. logmsg("---- WARNING: This drive contains the LIDO driver and may cause issues.");
  76. }
  77. return result;
  78. }
  79. // Called from ZuluSCSI_disk after image is initalized.
  80. static void macQuirksSanityCheck(image_config_t *img)
  81. {
  82. if(g_scsi_settings.getDevice(img->scsiId & 7)->disableMacSanityCheck)
  83. {
  84. dbgmsg("---- Skipping Mac sanity check due to DisableMacSanityCheck");
  85. return;
  86. }
  87. if(img->deviceType == S2S_CFG_FIXED)
  88. {
  89. if(!isValidMacintoshImage(img))
  90. {
  91. logmsg("---- WARNING: This image does not appear to be a valid Macintosh disk image.");
  92. }
  93. else
  94. {
  95. dbgmsg("---- Valid Macintosh disk image detected.");
  96. }
  97. }
  98. // Macintosh hosts reserve ID 7, so warn the user this configuration wont work
  99. if((img->scsiId & S2S_CFG_TARGET_ID_BITS) == 7)
  100. {
  101. logmsg("---- WARNING: Quirks set to Apple so can not use SCSI ID 7!");
  102. }
  103. }
  104. void quirksCheck(image_config_t *img)
  105. {
  106. if (img->quirks == S2S_CFG_QUIRKS_APPLE)
  107. {
  108. macQuirksSanityCheck(img);
  109. }
  110. }