QuirksCheck.cpp 4.2 KB

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