Skip to content

Commit 6126b4e

Browse files
Finalizing Legacy BIOS Support
1 parent 3a46398 commit 6126b4e

File tree

20 files changed

+451
-86
lines changed

20 files changed

+451
-86
lines changed

WinDiskWriter/AppDelegate/AppDelegate.m

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#import "AppDelegate.h"
1010
#import "NSString+Common.h"
1111
#import "NSError+Common.h"
12+
#import "HelperFunctions.h"
1213

1314
#import "Constants.h"
1415

@@ -20,19 +21,20 @@ @implementation AppDelegate {
2021
AboutWindow *aboutWindow;
2122

2223
NSMenuItem *quitMenuItem;
24+
NSMenuItem *closeMenuItem;
2325
}
2426

2527
- (void)setupMenuItems {
2628
NSMenu *menuBar = [[NSMenu alloc]init];
27-
[NSApp setMainMenu:menuBar];
29+
[NSApp setMainMenu: menuBar];
2830

2931
NSMenuItem *mainMenuBarItem = [[NSMenuItem alloc] init]; {
3032
[menuBar addItem:mainMenuBarItem];
3133

3234
NSMenu *mainItemsMenu = [[NSMenu alloc] init]; {
3335
[mainMenuBarItem setSubmenu:mainItemsMenu];
3436

35-
NSMenuItem* aboutMenuItem = [[NSMenuItem alloc] initWithTitle: MENU_ITEM_ABOUT_TITLE
37+
NSMenuItem *aboutMenuItem = [[NSMenuItem alloc] initWithTitle: MENU_ITEM_ABOUT_TITLE
3638
action: @selector(showAboutWindow)
3739
keyEquivalent: @""]; {
3840
[mainItemsMenu addItem: aboutMenuItem];
@@ -53,51 +55,73 @@ - (void)setupMenuItems {
5355
[menuBar addItem:editMenuBarItem];
5456

5557
NSMenu *editMenu = [[NSMenu alloc] initWithTitle: MENU_EDIT_TITLE]; {
56-
[editMenuBarItem setSubmenu:editMenu];
58+
[editMenuBarItem setSubmenu: editMenu];
5759

58-
NSMenuItem* cutMenuItem = [[NSMenuItem alloc] initWithTitle: MENU_ITEM_CUT_TITLE
60+
NSMenuItem *cutMenuItem = [[NSMenuItem alloc] initWithTitle: MENU_ITEM_CUT_TITLE
5961
action: @selector(cut:)
6062
keyEquivalent: @"x"]; {
61-
[editMenu addItem:cutMenuItem];
63+
[editMenu addItem: cutMenuItem];
6264
}
6365

64-
NSMenuItem* copyMenuItem = [[NSMenuItem alloc] initWithTitle: MENU_ITEM_COPY_TITLE
66+
NSMenuItem *copyMenuItem = [[NSMenuItem alloc] initWithTitle: MENU_ITEM_COPY_TITLE
6567
action: @selector(copy:)
6668
keyEquivalent: @"c"]; {
67-
[editMenu addItem:copyMenuItem];
69+
[editMenu addItem: copyMenuItem];
6870
}
6971

70-
NSMenuItem* pasteMenuItem = [[NSMenuItem alloc] initWithTitle: MENU_ITEM_PASTE_TITLE
72+
NSMenuItem *pasteMenuItem = [[NSMenuItem alloc] initWithTitle: MENU_ITEM_PASTE_TITLE
7173
action: @selector(paste:)
7274
keyEquivalent: @"v"]; {
73-
[editMenu addItem:pasteMenuItem];
75+
[editMenu addItem: pasteMenuItem];
7476
}
7577

76-
NSMenuItem* selectAllMenuItem = [[NSMenuItem alloc] initWithTitle: MENU_ITEM_SELECT_ALL_TITLE
78+
NSMenuItem *selectAllMenuItem = [[NSMenuItem alloc] initWithTitle: MENU_ITEM_SELECT_ALL_TITLE
7779
action: @selector(selectAll:)
7880
keyEquivalent: @"a"]; {
79-
[editMenu addItem:selectAllMenuItem];
81+
[editMenu addItem: selectAllMenuItem];
8082
}
8183
}
8284

8385
}
8486

8587
NSMenuItem *windowMenuBarItem = [[NSMenuItem alloc] init]; {
86-
[menuBar addItem:windowMenuBarItem];
88+
[menuBar addItem: windowMenuBarItem];
8789

8890
NSMenu *windowMenu = [[NSMenu alloc] initWithTitle: MENU_WINDOW_TITLE]; {
89-
[windowMenuBarItem setSubmenu:windowMenu];
91+
[windowMenuBarItem setSubmenu: windowMenu];
9092

91-
NSMenuItem* minimizeMenuItem = [[NSMenuItem alloc] initWithTitle: MENU_MINIMIZE_TITLE
93+
closeMenuItem = [[NSMenuItem alloc] initWithTitle: @"Close"
94+
action: NULL
95+
keyEquivalent: @"w"]; {
96+
[windowMenu addItem: closeMenuItem];
97+
}
98+
99+
NSMenuItem *minimizeMenuItem = [[NSMenuItem alloc] initWithTitle: MENU_MINIMIZE_TITLE
92100
action: @selector(miniaturize:)
93101
keyEquivalent: @"m"]; {
94-
[windowMenu addItem:minimizeMenuItem];
102+
[windowMenu addItem: minimizeMenuItem];
95103
}
96104

97-
NSMenuItem* hideMenuItem = [[NSMenuItem alloc] initWithTitle: MENU_HIDE_TITLE
105+
NSMenuItem *hideMenuItem = [[NSMenuItem alloc] initWithTitle: MENU_HIDE_TITLE
98106
action: @selector(hide:)
99107
keyEquivalent: @"h"]; {
100-
[windowMenu addItem:hideMenuItem];
108+
[windowMenu addItem: hideMenuItem];
109+
}
110+
}
111+
}
112+
113+
NSMenuItem *supportMeMenuBarItem = [[NSMenuItem alloc] init]; {
114+
[menuBar addItem: supportMeMenuBarItem];
115+
116+
NSMenu *supportMeMenu = [[NSMenu alloc] initWithTitle: MENU_DONATE_ME_TITLE]; {
117+
[supportMeMenuBarItem setSubmenu: supportMeMenu];
118+
119+
NSMenuItem *openDonationURLMenuItem = [[NSMenuItem alloc] initWithTitle: MENU_ITEM_OPEN_DONATION_WEB_PAGE_TITLE
120+
action: @selector(openDonationsPage)
121+
keyEquivalent: @"d"]; {
122+
[openDonationURLMenuItem setTarget: [HelperFunctions class]];
123+
124+
[supportMeMenu addItem: openDonationURLMenuItem];
101125
}
102126
}
103127
}
@@ -127,7 +151,8 @@ - (void)setupWindows {
127151
padding: CHILD_CONTENT_SPACING
128152
paddingIsTitleBarAware: YES
129153
aboutWindow: aboutWindow
130-
quitMenuItem: quitMenuItem];
154+
quitMenuItem: quitMenuItem
155+
closeMenuItem: closeMenuItem];
131156

132157
[mainWindow setMinSize: minWindowSize];
133158
[mainWindow setMaxSize: maxWindowSize];

WinDiskWriter/Classes/DiskWriter/DiskWriter.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ typedef NS_ENUM(NSUInteger, DWOperationType) {
3030
/* Optional for Windows 11 and up.
3131
Removes TPM and Secure Boot requirements by setting
3232
the types of all images inside install(.wim)/(.esd) to "Server" */
33-
DWOperationTypePatchWindowsInstallerRequirements
33+
DWOperationTypePatchWindowsInstallerRequirements,
34+
DWOperationTypeInstallLegacyBootSector
3435
};
3536

3637
@interface DiskWriter: NSObject
@@ -51,7 +52,7 @@ NS_ASSUME_NONNULL_BEGIN
5152
destinationPath: (NSString *)destinationPath
5253
destinationDiskManager: (DiskManager *)destinationDiskManager;
5354

54-
typedef DWAction (^ChainedCallbackAction)(DWFile *dwFile, uint64 copiedBytes, DWOperationType operationType, DWOperationResult operationResult, NSError *error);
55+
typedef DWAction (^ChainedCallbackAction)(DWFile *dwFile, uint64 copiedBytes, DWOperationType operationType, DWOperationResult operationResult, NSError *_Nullable error);
5556

5657
- (BOOL)startWritingWithError: (NSError **)error
5758
progressCallback: (ChainedCallbackAction)progressCallback;

WinDiskWriter/Classes/DiskWriter/DiskWriter.m

Lines changed: 50 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,17 @@ + (NSString *)bootloaderGrldrFilePath {
6464
inDirectory: BUNDLE_BOOTLOADER_SUBDIRECTORY_NAME];
6565
}
6666

67+
+ (NSString *)bootloaderMenuFilePath {
68+
return [[NSBundle mainBundle] pathForResource: @"menu"
69+
ofType: @"lst"
70+
inDirectory: BUNDLE_BOOTLOADER_SUBDIRECTORY_NAME];
71+
}
72+
6773
- (BOOL)writeLegacyBootSectorWithError: (NSError **)error {
6874
NSString *bootloaderMBRFilePath = [DiskWriter bootloaderMBRFilePath];
6975
NSString *bootloaderGrldrFilePath = [DiskWriter bootloaderGrldrFilePath];
70-
76+
NSString *bootloaderMenuFilePath = [DiskWriter bootloaderMenuFilePath];
77+
7178
DiskInfo *destinationDiskInfo = [self.destinationDiskManager diskInfo];
7279
NSString *bsdFullPath = [destinationDiskInfo BSDFullPath];
7380

@@ -83,25 +90,38 @@ - (BOOL)writeLegacyBootSectorWithError: (NSError **)error {
8390
return NO;
8491
}
8592

86-
if(![localFileManager fileExistsAtPathAndNotAFolder: bootloaderMBRFilePath]) {
93+
if(![localFileManager fileExistsAtPathAndNotAFolder: bootloaderGrldrFilePath]) {
8794
*error = [NSError errorWithStringValue: @"Bootloader Grldr file doesn't exist."];
8895

8996
return NO;
9097
}
9198

92-
// Copying grldr (second-stage boot for the grub4dos)
93-
NSError *grldrCopyError = NULL;
94-
NSString *grldrDestinationPath = [self.destinationPath stringByAppendingPathComponent: bootloaderGrldrFilePath.lastPathComponent];
95-
[localFileManager copyItemAtPath: bootloaderGrldrFilePath
96-
toPath: grldrDestinationPath
97-
error: &grldrCopyError];
99+
if(![localFileManager fileExistsAtPathAndNotAFolder: bootloaderMenuFilePath]) {
100+
*error = [NSError errorWithStringValue: @"Bootloader Menu file doesn't exist."];
101+
102+
return NO;
103+
}
98104

99-
if (grldrCopyError != NULL) {
100-
NSString *errorString = [NSString stringWithFormat: @"Can't install grldr to the destination device (%@).", grldrCopyError.stringValue];
105+
for (NSString *currentFile in @[bootloaderGrldrFilePath, bootloaderMenuFilePath]) {
106+
NSError *bootloaderFileCopyError = NULL;
101107

102-
*error = [NSError errorWithStringValue: errorString];
108+
NSString *destinationPath = [self.destinationPath stringByAppendingPathComponent: currentFile.lastPathComponent];
103109

104-
return NO;
110+
[localFileManager copyItemAtPath: currentFile
111+
toPath: destinationPath
112+
error: &bootloaderFileCopyError];
113+
114+
if (bootloaderFileCopyError != NULL) {
115+
NSString *errorString = [
116+
NSString stringWithFormat: @"Can't copy %@ to the destination device (%@).",
117+
currentFile.lastPathComponent,
118+
bootloaderFileCopyError.stringValue
119+
];
120+
121+
*error = [NSError errorWithStringValue: errorString];
122+
123+
return NO;
124+
}
105125
}
106126

107127
// Unmounting the destination device in order to install the boot sector.
@@ -664,18 +684,31 @@ - (BOOL)startWritingWithError: (NSError **)error
664684
postBootloaderExtract:
665685

666686
if (self.installLegacyBoot) {
687+
DWFile *legacyBootloaderDWFile = [[DWFile alloc] initWithSourcePath: @"[Legacy Bootloader]"];
688+
689+
switch (progressCallback(legacyBootloaderDWFile, 0, DWOperationTypeInstallLegacyBootSector, DWOperationResultStart, NULL)) {
690+
case DWActionContinue:
691+
break;
692+
case DWActionSkip:
693+
goto postLegacyBootloaderInstall;
694+
case DWActionStop:
695+
return NO;
696+
}
697+
667698
NSError *installLegacyBootError = NULL;
668699

669700
BOOL installLegacyBootResult = [self writeLegacyBootSectorWithError: &installLegacyBootError];
670701

671-
// Temporary debug print
672-
if (installLegacyBootError != NULL) {
673-
printf("Install Legacy Boot ERROR!: %s\n", installLegacyBootError.stringValue.UTF8String);
674-
}
702+
progressCallback(legacyBootloaderDWFile, 0, DWOperationTypeInstallLegacyBootSector, (installLegacyBootError == NULL) ? DWOperationResultSuccess : DWOperationResultFailure, installLegacyBootError);
675703

676-
printf("Legacy install result: %s\n", installLegacyBootResult ? "Success" : "Failure");
704+
if (!installLegacyBootResult) {
705+
// We don't need to continue since it's the last step
706+
return NO;
707+
}
677708
}
678709

710+
postLegacyBootloaderInstall:
711+
679712
return YES;
680713
}
681714

WinDiskWriter/Classes/HelperFunctions/HelperFunctions.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@ NS_ASSUME_NONNULL_BEGIN
1717

1818
- (instancetype)init NS_UNAVAILABLE;
1919

20+
+ (BOOL)hasElevatedRights;
21+
22+
+ (void)openDonationsPage;
23+
2024
+ (void)printTimeElapsedWhenRunningCode: (NSString *)title
2125
operation: (void (^)(void))operation;
2226

23-
+ (BOOL)hasElevatedRights;
24-
2527
+ (BOOL)restartWithElevatedPermissionsWithError: (NSError *_Nonnull *_Nonnull)error;
2628

2729
+ (NSString *)randomStringWithLength: (UInt64)requiredLength;
@@ -34,6 +36,7 @@ NS_ASSUME_NONNULL_BEGIN
3436
error: (NSError *_Nullable *_Nullable)error;
3537

3638
+ (NSString *)unitFormattedSizeFor: (UInt64)bytes;
39+
3740
@end
3841

3942
NS_ASSUME_NONNULL_END

WinDiskWriter/Classes/HelperFunctions/HelperFunctions.m

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,19 @@
1414
#import "Constants.h"
1515
#import "HDIUtil.h"
1616

17-
NSString const *MSDOSCompliantSymbols = @"ABCDEFGHIJKLMNOPQRSTUVWXZY0123456789";
17+
NSString * const MSDOSCompliantSymbols = @"ABCDEFGHIJKLMNOPQRSTUVWXZY0123456789";
1818

1919
@implementation HelperFunctions
2020

2121
+ (BOOL)hasElevatedRights {
2222
return geteuid() == 0;
2323
}
2424

25+
+ (void)openDonationsPage {
26+
NSURL *url = [NSURL URLWithString: @"https://github.com/TechUnRestricted/windiskwriter"];
27+
[[NSWorkspace sharedWorkspace] openURL: url];
28+
}
29+
2530
+ (void)printTimeElapsedWhenRunningCode: (NSString *)title
2631
operation: (void (^)(void))operation {
2732
CFAbsoluteTime startTime = CFAbsoluteTimeGetCurrent();

WinDiskWriter/Constants/Constants.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ extern NSString * const MENU_WINDOW_TITLE;
8181
extern NSString * const MENU_MINIMIZE_TITLE;
8282
extern NSString * const MENU_HIDE_TITLE;
8383

84+
// ❤️ Support Me ❤️ Menu
85+
extern NSString * const MENU_DONATE_ME_TITLE;
86+
extern NSString * const MENU_ITEM_OPEN_DONATION_WEB_PAGE_TITLE;
87+
8488
@end
8589

8690
#endif /* Constants_h */

WinDiskWriter/Constants/Constants.m

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,7 @@ @implementation Constants
7171
NSString * const MENU_MINIMIZE_TITLE = @"Minimize";
7272
NSString * const MENU_HIDE_TITLE = @"Hide";
7373

74+
NSString * const MENU_DONATE_ME_TITLE = @"❤️ Donate Me ❤️";
75+
NSString * const MENU_ITEM_OPEN_DONATION_WEB_PAGE_TITLE = @"Open Donation Web Page";
76+
7477
@end

WinDiskWriter/Views/LabelView/LabelView.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@ NS_ASSUME_NONNULL_BEGIN
1212

1313
@interface LabelView : NSTextField
1414

15-
- (instancetype)init;
15+
- (BOOL)isClickActionRegistered;
16+
17+
- (void)unregisterClickAction;
18+
19+
- (void)registerClickWithTarget: (id)target
20+
selector: (SEL)selector;
1621

1722
@end
1823

0 commit comments

Comments
 (0)