-
Notifications
You must be signed in to change notification settings - Fork 0
/
boot.c
401 lines (339 loc) · 15.1 KB
/
boot.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/******************************* LICENCE **************************************
* Any code in this file may be redistributed or modified under the terms of
* the GNU General Public Licence as published by the Free Software
* Foundation; version 2 of the licence.
****************************** END LICENCE ***********************************/
/******************************************************************************
* Author:
* Andrew Smith, http://littlesvr.ca/misc/contactandrew.php
*
* Contributors:
*
******************************************************************************/
#include <gtk/gtk.h>
#include <string.h>
#include <libintl.h>
#include "isomaster.h"
extern GtkWidget* GBLmainWindow;
extern VolInfo GBLvolInfo;
extern bool GBLisoPaneActive;
extern GtkWidget* GBLisoTreeView;
extern char* GBLisoCurrentDir;
extern AppSettings GBLappSettings;
extern bool GBLisoChangesProbable;
void addBootRecordFromFileCbk(GtkButton *button, gpointer bootRecordType)
{
GtkWidget* dialog;
GtkWidget* warningDialog;
char* filename;
int rc;
if(!GBLisoPaneActive)
/* no iso open */
return;
dialog = gtk_file_chooser_dialog_new(_("Choose Boot Record File"),
NULL,
GTK_FILE_CHOOSER_ACTION_OPEN,
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
NULL);
if(GBLappSettings.lastBootRecordDir != NULL)
gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), GBLappSettings.lastBootRecordDir);
if(gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT)
{
filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
/* RECORD last boot record dir */
char* lastBootRecordDir = gtk_file_chooser_get_current_folder(GTK_FILE_CHOOSER(dialog));
if(GBLappSettings.lastBootRecordDir != NULL &&
strlen(lastBootRecordDir) > strlen(GBLappSettings.lastBootRecordDir))
{
free(GBLappSettings.lastBootRecordDir);
GBLappSettings.lastBootRecordDir = NULL;
}
if(GBLappSettings.lastBootRecordDir == NULL)
GBLappSettings.lastBootRecordDir = malloc(strlen(lastBootRecordDir) + 1);
strcpy(GBLappSettings.lastBootRecordDir, lastBootRecordDir);
g_free(lastBootRecordDir);
/* END RECORD last boot record dir */
rc = bk_add_boot_record(&GBLvolInfo, filename, (int)(long long)bootRecordType);
if(rc <= 0)
{
warningDialog = gtk_message_dialog_new(GTK_WINDOW(GBLmainWindow),
GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_ERROR,
GTK_BUTTONS_CLOSE,
_("Failed to add boot record: '%s'"),
bk_get_error_string(rc));
gtk_window_set_modal(GTK_WINDOW(warningDialog), TRUE);
gtk_dialog_run(GTK_DIALOG(warningDialog));
gtk_widget_destroy(warningDialog);
}
else
GBLisoChangesProbable = true;
g_free(filename);
}
gtk_widget_destroy(dialog);
}
void deleteBootRecordCbk(GtkButton *button, gpointer data)
{
GtkWidget* dialog;
if(!GBLisoPaneActive)
/* no iso open */
return;
if(GBLvolInfo.bootMediaType == BOOT_MEDIA_NONE)
{
dialog = gtk_message_dialog_new(GTK_WINDOW(GBLmainWindow),
GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_ERROR,
GTK_BUTTONS_CLOSE,
_("No boot to delete"));
gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog);
return;
}
else
{
bk_delete_boot_record(&GBLvolInfo);
dialog = gtk_message_dialog_new(GTK_WINDOW(GBLmainWindow),
GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_INFO,
GTK_BUTTONS_CLOSE,
_("Boot record deleted"));
gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog);
GBLisoChangesProbable = true;
}
}
void extractBootRecordCbk(GtkButton *button, gpointer data)
{
GtkWidget* dialog;
GtkWidget* warningDialog;
int dialogResponse;
char* filename = NULL;
int rc;
if(!GBLisoPaneActive)
/* no iso open */
return;
if(GBLvolInfo.bootMediaType == BOOT_MEDIA_NONE)
{
dialog = gtk_message_dialog_new(GTK_WINDOW(GBLmainWindow),
GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_ERROR,
GTK_BUTTONS_CLOSE,
_("No boot record read from original or set on image"));
gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog);
return;
}
dialog = gtk_file_chooser_dialog_new(_("Save a copy of the boot record"),
NULL,
GTK_FILE_CHOOSER_ACTION_SAVE,
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
NULL);
if(GBLappSettings.lastBootRecordDir != NULL)
gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), GBLappSettings.lastBootRecordDir);
dialogResponse = gtk_dialog_run(GTK_DIALOG(dialog));
if(dialogResponse == GTK_RESPONSE_ACCEPT)
{
filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
/* RECORD last boot record dir */
char* lastBootRecordDir = gtk_file_chooser_get_current_folder(GTK_FILE_CHOOSER(dialog));
if(GBLappSettings.lastBootRecordDir != NULL &&
strlen(lastBootRecordDir) > strlen(GBLappSettings.lastBootRecordDir))
{
free(GBLappSettings.lastBootRecordDir);
GBLappSettings.lastBootRecordDir = NULL;
}
if(GBLappSettings.lastBootRecordDir == NULL)
GBLappSettings.lastBootRecordDir = malloc(strlen(lastBootRecordDir) + 1);
strcpy(GBLappSettings.lastBootRecordDir, lastBootRecordDir);
g_free(lastBootRecordDir);
/* END RECORD last boot record dir */
}
gtk_widget_destroy(dialog);
if(dialogResponse == GTK_RESPONSE_ACCEPT)
{
rc = bk_extract_boot_record(&GBLvolInfo, filename, 0644);
if(rc <= 0)
{
warningDialog = gtk_message_dialog_new(GTK_WINDOW(GBLmainWindow),
GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_ERROR,
GTK_BUTTONS_CLOSE,
_("Failed to extract boot record: '%s'"),
bk_get_error_string(rc));
gtk_dialog_run(GTK_DIALOG(warningDialog));
gtk_widget_destroy(warningDialog);
}
g_free(filename);
}
}
void setFileAsBootRecordCbk(GtkButton *button, gpointer data)
{
GtkTreeSelection* selection;
GtkWidget* dialog;
if(!GBLisoPaneActive)
/* no iso open */
return;
selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(GBLisoTreeView));
if(gtk_tree_selection_count_selected_rows(selection) == 0)
{
dialog = gtk_message_dialog_new(GTK_WINDOW(GBLmainWindow),
GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_ERROR,
GTK_BUTTONS_CLOSE,
_("Please select a file in the ISO browser to "
"use as the boot record"));
gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog);
return;
}
if(gtk_tree_selection_count_selected_rows(selection) > 1)
{
dialog = gtk_message_dialog_new(GTK_WINDOW(GBLmainWindow),
GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_ERROR,
GTK_BUTTONS_CLOSE,
_("Please select no more than one file in the ISO browser"));
gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog);
return;
}
gtk_tree_selection_selected_foreach(selection, setFileAsBootRecordRowCbk, NULL);
}
void setFileAsBootRecordRowCbk(GtkTreeModel* model, GtkTreePath* path,
GtkTreeIter* iterator, gpointer data)
{
int fileType;
char* itemName;
char* fullItemName;
GtkWidget* dialog;
int rc;
gtk_tree_model_get(model, iterator, COLUMN_HIDDEN_TYPE, &fileType,
COLUMN_FILENAME, &itemName, -1);
if(fileType != FILE_TYPE_REGULAR)
{
dialog = gtk_message_dialog_new(GTK_WINDOW(GBLmainWindow),
GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_ERROR,
GTK_BUTTONS_CLOSE,
_("Item selected is not a regular file and cannot"
" be used as a boot record"));
gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog);
g_free(itemName);
return;
}
fullItemName = (char*)malloc(strlen(GBLisoCurrentDir) + strlen(itemName) + 1);
if(fullItemName == NULL)
fatalError("setFileAsBootRecordRowCbk(): malloc("
"strlen(GBLisoCurrentDir) + strlen(itemName) + 1) failed");
strcpy(fullItemName, GBLisoCurrentDir);
strcat(fullItemName, itemName);
rc = bk_set_boot_file(&GBLvolInfo, fullItemName);
if(rc <= 0)
{
dialog = gtk_message_dialog_new(GTK_WINDOW(GBLmainWindow),
GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_ERROR,
GTK_BUTTONS_CLOSE,
_("Failed to set %s as boot record: '%s'"),
itemName,
bk_get_error_string(rc));
gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog);
}
else
GBLisoChangesProbable = true;
g_free(itemName);
}
void showBootInfoCbk(GtkButton *button, gpointer data)
{
GtkWidget* dialog;
GtkWidget* vBox;
//~ GtkWidget* hBox;
GtkWidget* label;
char str[100];
if(!GBLisoPaneActive)
/* no iso open */
return;
if(GBLvolInfo.bootMediaType == BOOT_MEDIA_NONE)
{
dialog = gtk_message_dialog_new(GTK_WINDOW(GBLmainWindow),
GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_INFO,
GTK_BUTTONS_CLOSE,
_("No boot record read from original or set on image"));
gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog);
return;
}
dialog = gtk_dialog_new_with_buttons(_("Boot Record Information"),
GTK_WINDOW(GBLmainWindow),
GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_STOCK_OK,
GTK_RESPONSE_NONE,
NULL);
//gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
g_signal_connect(dialog, "close", G_CALLBACK(rejectDialogCbk), NULL);
vBox = gtk_vbox_new(TRUE, 5);
gtk_container_add(GTK_CONTAINER(gtk_get_dialog_vbox(GTK_DIALOG(dialog))), vBox);
gtk_widget_show(vBox);
if(GBLvolInfo.bootMediaType == BOOT_MEDIA_NO_EMULATION)
sprintf(str, _("Boot record type: No Emulation"));
else if(GBLvolInfo.bootMediaType == BOOT_MEDIA_1_2_FLOPPY)
sprintf(str, _("Boot record type: 1200KiB Floppy"));
else if(GBLvolInfo.bootMediaType == BOOT_MEDIA_1_44_FLOPPY)
sprintf(str, _("Boot record type: 1440KiB Floppy"));
else if(GBLvolInfo.bootMediaType == BOOT_MEDIA_2_88_FLOPPY)
sprintf(str, _("Boot record type: 2880KiB Floppy"));
else
sprintf(str, "Boot record type: error");
label = gtk_label_new(str);
gtk_box_pack_start(GTK_BOX(vBox), label, TRUE, TRUE, 0);
gtk_widget_show(label);
if(GBLvolInfo.bootMediaType == BOOT_MEDIA_NO_EMULATION)
{
snprintf(str, 100, _("Size: %d bytes"), GBLvolInfo.bootRecordSize);
label = gtk_label_new(str);
gtk_box_pack_start(GTK_BOX(vBox), label, TRUE, TRUE, 0);
gtk_widget_show(label);
}
if(GBLvolInfo.bootRecordIsVisible)
/* get this info from the original file */
{
if(GBLvolInfo.bootRecordOnImage->onImage)
snprintf(str, 100, _("Location: on original image at 0x%llX"), (long long unsigned)GBLvolInfo.bootRecordOnImage->position);
else
snprintf(str, 100, _("Location: to be added from '%s'"), GBLvolInfo.bootRecordOnImage->pathAndName);
}
else
{
if(GBLvolInfo.bootRecordIsOnImage)
snprintf(str, 100, _("Location: on original image at 0x%llX"), (long long unsigned)GBLvolInfo.bootRecordOffset);
else
snprintf(str, 100, _("Location: to be added from '%s'"), GBLvolInfo.bootRecordPathAndName);
}
label = gtk_label_new(str);
gtk_box_pack_start(GTK_BOX(vBox), label, TRUE, TRUE, 0);
gtk_widget_show(label);
if(GBLvolInfo.bootRecordIsVisible)
snprintf(str, 100, _("Is visible on image as '%s'"), GBLvolInfo.bootRecordOnImage->base.name);
else
snprintf(str, 100, _("Is not visible on image"));
label = gtk_label_new(str);
gtk_box_pack_start(GTK_BOX(vBox), label, TRUE, TRUE, 0);
gtk_widget_show(label);
/* down here so it's centered properly over the main window */
//gtk_widget_show(dialog);
gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog);
}