forked from pbatard/rufus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrufus.c
executable file
·4176 lines (3926 loc) · 166 KB
/
rufus.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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Rufus: The Reliable USB Formatting Utility
* Copyright © 2011-2024 Pete Batard <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/* Memory leaks detection - define _CRTDBG_MAP_ALLOC as preprocessor macro */
#ifdef _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#endif
#include <windows.h>
#include <windowsx.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <winioctl.h>
#include <shlobj.h>
#include <process.h>
#include <dwmapi.h>
#include <dbt.h>
#include <io.h>
#include <getopt.h>
#include <assert.h>
#include <delayimp.h>
#include "rufus.h"
#include "format.h"
#include "missing.h"
#include "resource.h"
#include "msapi_utf8.h"
#include "localization.h"
#include "ui.h"
#include "re.h"
#include "vhd.h"
#include "wue.h"
#include "drive.h"
#include "settings.h"
#include "bled/bled.h"
#include "cdio/logging.h"
#include "../res/grub/grub_version.h"
#include "../res/grub2/grub2_version.h"
enum bootcheck_return {
BOOTCHECK_PROCEED = 0,
BOOTCHECK_CANCEL = -1,
BOOTCHECK_DOWNLOAD_ERROR = -2,
BOOTCHECK_GENERAL_ERROR = -3,
};
static const char* cmdline_hogger = ".\\rufus.com";
static const char* ep_reg = "Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer";
static const char* vs_reg = "Software\\Microsoft\\VisualStudio";
static const char* arch_name[ARCH_MAX] = {
"unknown", "x86_32", "x86_64", "ARM", "ARM64", "IA64", "RISC-V 64", "LoongArch 64", "EBC" };
static BOOL existing_key = FALSE; // For LGP set/restore
static BOOL size_check = TRUE;
static BOOL log_displayed = FALSE;
static BOOL img_provided = FALSE;
static BOOL user_notified = FALSE;
static BOOL relaunch = FALSE;
static BOOL dont_display_image_name = FALSE;
static BOOL dont_process_dbt_devnodes = FALSE;
static BOOL user_changed_label = FALSE;
static BOOL user_deleted_rufus_dir = FALSE;
static BOOL app_changed_label = FALSE;
static BOOL allowed_filesystem[FS_MAX] = { 0 };
static int64_t last_iso_blocking_status;
static int selected_pt = -1, selected_fs = FS_UNKNOWN, preselected_fs = FS_UNKNOWN;
static int image_index = 0, select_index = 0;
static RECT relaunch_rc = { -65536, -65536, 0, 0};
static HWND hSelectImage = NULL, hStart = NULL;
static char szTimer[12] = "00:00:00";
static unsigned int timer;
static char uppercase_select[2][64], uppercase_start[64], uppercase_close[64], uppercase_cancel[64];
extern HANDLE update_check_thread, wim_thread;
extern BOOL enable_iso, enable_joliet, enable_rockridge, enable_extra_hashes, is_bootloader_revoked;
extern BOOL validate_md5sum, cpu_has_sha1_accel, cpu_has_sha256_accel;
extern BYTE* fido_script;
extern HWND hFidoDlg;
extern uint8_t* grub2_buf;
extern long grub2_len;
extern char* szStatusMessage;
extern const char* old_c32_name[NB_OLD_C32];
extern const char* cert_name[3];
extern const char* FileSystemLabel[FS_MAX];
extern const char *bootmgr_efi_name, *efi_dirname, *efi_bootname[ARCH_MAX];
/*
* Globals
*/
OPENED_LIBRARIES_VARS;
RUFUS_UPDATE update = { { 0,0,0 },{ 0,0 }, NULL, NULL };
HINSTANCE hMainInstance;
HWND hMainDialog, hMultiToolbar, hSaveToolbar, hHashToolbar, hAdvancedDeviceToolbar, hAdvancedFormatToolbar, hUpdatesDlg = NULL;
HFONT hInfoFont;
uint8_t image_options = IMOP_WINTOGO;
uint16_t rufus_version[3], embedded_sl_version[2];
uint32_t dur_mins, dur_secs;
loc_cmd* selected_locale = NULL;
WORD selected_langid = MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT);
DWORD MainThreadId;
USHORT NativeMachine = IMAGE_FILE_MACHINE_UNKNOWN;
HWND hDeviceList, hPartitionScheme, hTargetSystem, hFileSystem, hClusterSize, hLabel, hBootType, hNBPasses, hLog = NULL;
HWND hImageOption, hLogDialog = NULL, hProgress = NULL;
HANDLE dialog_handle = NULL, format_thread = NULL;
BOOL is_x86_64, use_own_c32[NB_OLD_C32] = { FALSE, FALSE }, mbr_selected_by_user = FALSE, lock_drive = TRUE;
BOOL op_in_progress = TRUE, right_to_left_mode = FALSE, has_uefi_csm = FALSE, its_a_me_mario = FALSE;
BOOL enable_HDDs = FALSE, enable_VHDs = TRUE, enable_ntfs_compression = FALSE, no_confirmation_on_cancel = FALSE;
BOOL advanced_mode_device, advanced_mode_format, allow_dual_uefi_bios, detect_fakes, enable_vmdk, force_large_fat32;
BOOL usb_debug, use_fake_units, preserve_timestamps = FALSE, fast_zeroing = FALSE, app_changed_size = FALSE;
BOOL zero_drive = FALSE, list_non_usb_removable_drives = FALSE, enable_file_indexing, large_drive = FALSE;
BOOL write_as_image = FALSE, write_as_esp = FALSE, use_vds = FALSE, ignore_boot_marker = FALSE;
BOOL appstore_version = FALSE, is_vds_available = TRUE, persistent_log = FALSE, has_ffu_support = FALSE;
BOOL expert_mode = FALSE, use_rufus_mbr = TRUE;
float fScale = 1.0f;
int dialog_showing = 0, selection_default = BT_IMAGE, persistence_unit_selection = -1, imop_win_sel = 0;
int default_fs, fs_type, boot_type, partition_type, target_type;
int force_update = 0, default_thread_priority = THREAD_PRIORITY_ABOVE_NORMAL;
char szFolderPath[MAX_PATH], app_dir[MAX_PATH], system_dir[MAX_PATH], temp_dir[MAX_PATH], sysnative_dir[MAX_PATH];
char app_data_dir[MAX_PATH], user_dir[MAX_PATH], cur_dir[MAX_PATH];
char embedded_sl_version_str[2][12] = { "?.??", "?.??" };
char embedded_sl_version_ext[2][32];
char ClusterSizeLabel[MAX_CLUSTER_SIZES][64];
char msgbox[1024], msgbox_title[32], *ini_file = NULL, *image_path = NULL, *short_image_path;
char *archive_path = NULL, image_option_txt[128], *fido_url = NULL, *save_image_type = NULL;
char* sbat_level_txt = NULL;
StrArray BlockingProcessList, ImageList;
// Number of steps for each FS for FCC_STRUCTURE_PROGRESS
const int nb_steps[FS_MAX] = { 5, 5, 12, 1, 10, 1, 1, 1, 1 };
const char* flash_type[BADLOCKS_PATTERN_TYPES] = { "SLC", "MLC", "TLC" };
RUFUS_DRIVE rufus_drive[MAX_DRIVES] = { 0 };
sbat_entry_t* sbat_entries = NULL;
// TODO: Remember to update copyright year in stdlg's AboutCallback() WM_INITDIALOG,
// localization_data.sh and the .rc when the year changes!
// Fill in the cluster size names
static void SetClusterSizeLabels(void)
{
unsigned int i, j, msg_id;
safe_sprintf(ClusterSizeLabel[0], 64, "%s", lmprintf(MSG_029));
for (i=512, j=1, msg_id=MSG_026; j<MAX_CLUSTER_SIZES; i<<=1, j++) {
if (i > 8192) {
i /= 1024;
msg_id++;
}
safe_sprintf(ClusterSizeLabel[j], 64, "%d %s", i, lmprintf(msg_id));
}
}
static void SetAllowedFileSystems(void)
{
int i;
memset(allowed_filesystem, 0, sizeof(allowed_filesystem));
// Nothing is allowed if we don't have a drive
if (ComboBox_GetCurSel(hDeviceList) < 0)
return;
switch (selection_default) {
case BT_NON_BOOTABLE:
for (i = 0; i < FS_MAX; i++)
allowed_filesystem[i] = TRUE;
break;
case BT_MSDOS:
case BT_FREEDOS:
allowed_filesystem[FS_FAT16] = TRUE;
allowed_filesystem[FS_FAT32] = TRUE;
break;
case BT_IMAGE:
allowed_filesystem[FS_NTFS] = TRUE;
// Don't allow anything besides NTFS if the image has a >4GB file or explicitly requires NTFS
if ((image_path != NULL) && (img_report.has_4GB_file || img_report.needs_ntfs))
break;
if (!HAS_WINDOWS(img_report) || (target_type != TT_BIOS) || allow_dual_uefi_bios) {
if (!HAS_WINTOGO(img_report) || (ComboBox_GetCurItemData(hImageOption) != IMOP_WIN_TO_GO)) {
allowed_filesystem[FS_FAT16] = TRUE;
allowed_filesystem[FS_FAT32] = TRUE;
}
}
break;
case BT_GRUB2:
allowed_filesystem[FS_EXT2] = TRUE;
allowed_filesystem[FS_EXT3] = TRUE;
allowed_filesystem[FS_EXT4] = TRUE;
// Fall through
case BT_SYSLINUX_V6:
case BT_GRUB4DOS:
allowed_filesystem[FS_NTFS] = TRUE;
// Fall through
case BT_SYSLINUX_V4:
case BT_REACTOS:
allowed_filesystem[FS_FAT16] = TRUE;
allowed_filesystem[FS_FAT32] = TRUE;
break;
case BT_UEFI_NTFS:
allowed_filesystem[FS_NTFS] = TRUE;
allowed_filesystem[FS_EXFAT] = TRUE;
break;
}
}
// Populate the Boot selection dropdown
static void SetBootOptions(void)
{
char tmp[32];
IGNORE_RETVAL(ComboBox_ResetContent(hBootType));
IGNORE_RETVAL(ComboBox_SetItemData(hBootType, ComboBox_AddStringU(hBootType, lmprintf(MSG_279)), BT_NON_BOOTABLE));
IGNORE_RETVAL(ComboBox_SetItemData(hBootType, ComboBox_AddStringU(hBootType,
(image_path == NULL) ? lmprintf(MSG_281, lmprintf(MSG_280)) : short_image_path), BT_IMAGE));
image_index = 1;
IGNORE_RETVAL(ComboBox_SetItemData(hBootType, ComboBox_AddStringU(hBootType, "MS-DOS"), BT_MSDOS));
IGNORE_RETVAL(ComboBox_SetItemData(hBootType, ComboBox_AddStringU(hBootType, "FreeDOS"), BT_FREEDOS));
if (advanced_mode_device) {
static_sprintf(tmp, "Syslinux %s", embedded_sl_version_str[0]);
IGNORE_RETVAL(ComboBox_SetItemData(hBootType, ComboBox_AddStringU(hBootType, tmp), BT_SYSLINUX_V4));
static_sprintf(tmp, "Syslinux %s", embedded_sl_version_str[1]);
IGNORE_RETVAL(ComboBox_SetItemData(hBootType, ComboBox_AddStringU(hBootType, tmp), BT_SYSLINUX_V6));
IGNORE_RETVAL(ComboBox_SetItemData(hBootType, ComboBox_AddStringU(hBootType, "ReactOS"), BT_REACTOS));
IGNORE_RETVAL(ComboBox_SetItemData(hBootType, ComboBox_AddStringU(hBootType,
"Grub " GRUB2_PACKAGE_VERSION), BT_GRUB2));
IGNORE_RETVAL(ComboBox_SetItemData(hBootType, ComboBox_AddStringU(hBootType,
"Grub4DOS " GRUB4DOS_VERSION), BT_GRUB4DOS));
IGNORE_RETVAL(ComboBox_SetItemData(hBootType, ComboBox_AddStringU(hBootType, "UEFI:NTFS"), BT_UEFI_NTFS));
}
if ((!advanced_mode_device) && (selection_default >= BT_SYSLINUX_V4))
selection_default = BT_IMAGE;
SetComboEntry(hBootType, selection_default);
}
static void SetPartitionSchemeAndTargetSystem(BOOL only_target)
{
// MBR, GPT, SFD
BOOL allowed_partition_scheme[3] = { TRUE, TRUE, FALSE };
// BIOS, UEFI, DUAL
BOOL allowed_target_system[3] = { TRUE, TRUE, FALSE };
BOOL is_windows_to_go_selected;
if (!only_target)
IGNORE_RETVAL(ComboBox_ResetContent(hPartitionScheme));
IGNORE_RETVAL(ComboBox_ResetContent(hTargetSystem));
boot_type = (int)ComboBox_GetCurItemData(hBootType);
is_windows_to_go_selected = (boot_type == BT_IMAGE) && (image_path != NULL) && HAS_WINTOGO(img_report) &&
(ComboBox_GetCurItemData(hImageOption) == IMOP_WIN_TO_GO);
// If no device is selected, don't populate anything
if (ComboBox_GetCurSel(hDeviceList) < 0)
return;
switch (boot_type) {
case BT_NON_BOOTABLE:
allowed_partition_scheme[PARTITION_STYLE_SFD] = TRUE;
allowed_target_system[0] = FALSE;
allowed_target_system[1] = FALSE;
allowed_target_system[2] = TRUE;
break;
case BT_IMAGE:
if (image_path == NULL)
break;
// Check if image is EFI bootable
if (!IS_EFI_BOOTABLE(img_report)) {
allowed_partition_scheme[PARTITION_STYLE_GPT] = FALSE;
allowed_target_system[1] = FALSE;
break;
}
// Image is EFI bootable => set dual BIOS + UEFI and so on...
if (IS_BIOS_BOOTABLE(img_report)) {
if (!HAS_WINDOWS(img_report) || allow_dual_uefi_bios || is_windows_to_go_selected) {
allowed_target_system[0] = FALSE;
allowed_target_system[1] = TRUE;
allowed_target_system[2] = TRUE;
}
// Syslinux 4.x or earlier has no support for NTFS so if an image is using Syslinux 4.x only
// and has a 4 GB file (which forces us to use NTFS) then disable MBR altogether as we won't
// be able to make a working MBR install of Syslinux.
if (HAS_SYSLINUX(img_report) && (SL_MAJOR(img_report.sl_version) < 5) && img_report.has_4GB_file &&
!HAS_BOOTMGR(img_report) && !HAS_WINPE(img_report) && !HAS_GRUB(img_report))
allowed_partition_scheme[PARTITION_STYLE_MBR] = FALSE;
} else {
allowed_target_system[0] = FALSE;
}
break;
case BT_MSDOS:
case BT_FREEDOS:
case BT_SYSLINUX_V4:
case BT_SYSLINUX_V6:
case BT_REACTOS:
case BT_GRUB4DOS:
case BT_GRUB2:
allowed_partition_scheme[PARTITION_STYLE_GPT] = FALSE;
allowed_target_system[1] = FALSE;
break;
case BT_UEFI_NTFS:
allowed_target_system[0] = FALSE;
break;
}
if (!only_target) {
// Override partition type selection to GPT for drives larger than 2TB
if (SelectedDrive.DiskSize > 2 * TB)
selected_pt = PARTITION_STYLE_GPT;
// Try to reselect the current drive's partition scheme
int preferred_pt = SelectedDrive.PartitionStyle;
if (allowed_partition_scheme[PARTITION_STYLE_MBR])
IGNORE_RETVAL(ComboBox_SetItemData(hPartitionScheme,
ComboBox_AddStringU(hPartitionScheme, "MBR"), PARTITION_STYLE_MBR));
if (allowed_partition_scheme[PARTITION_STYLE_GPT])
IGNORE_RETVAL(ComboBox_SetItemData(hPartitionScheme,
ComboBox_AddStringU(hPartitionScheme, "GPT"), PARTITION_STYLE_GPT));
if (allowed_partition_scheme[PARTITION_STYLE_SFD])
IGNORE_RETVAL(ComboBox_SetItemData(hPartitionScheme,
ComboBox_AddStringU(hPartitionScheme, sfd_name), PARTITION_STYLE_SFD));
// Override the partition scheme according to the current
if (boot_type == BT_NON_BOOTABLE)
preferred_pt = (selected_pt >= 0) ? selected_pt : PARTITION_STYLE_MBR;
else if (boot_type == BT_UEFI_NTFS)
preferred_pt = (selected_pt >= 0) ? selected_pt : PARTITION_STYLE_GPT;
else if ((boot_type == BT_IMAGE) && (image_path != NULL) && (img_report.is_iso || img_report.is_windows_img)) {
if (HAS_WINDOWS(img_report) && img_report.has_efi)
preferred_pt = allow_dual_uefi_bios? PARTITION_STYLE_MBR :
((selected_pt >= 0) ? selected_pt : PARTITION_STYLE_GPT);
if (IS_DD_BOOTABLE(img_report))
preferred_pt = (selected_pt >= 0) ? selected_pt : PARTITION_STYLE_MBR;
}
SetComboEntry(hPartitionScheme, preferred_pt);
partition_type = (int)ComboBox_GetCurItemData(hPartitionScheme);
}
has_uefi_csm = FALSE;
if (allowed_target_system[0] && (partition_type != PARTITION_STYLE_GPT)) {
IGNORE_RETVAL(ComboBox_SetItemData(hTargetSystem,
ComboBox_AddStringU(hTargetSystem, lmprintf(MSG_031)), TT_BIOS));
has_uefi_csm = TRUE;
}
if (allowed_target_system[1] && !((partition_type == PARTITION_STYLE_MBR) && (boot_type == BT_IMAGE) && IS_BIOS_BOOTABLE(img_report) && IS_EFI_BOOTABLE(img_report)) )
IGNORE_RETVAL(ComboBox_SetItemData(hTargetSystem,
ComboBox_AddStringU(hTargetSystem, lmprintf(MSG_032)), TT_UEFI));
if (allowed_target_system[2] && ((partition_type != PARTITION_STYLE_GPT) || (boot_type == BT_NON_BOOTABLE)))
IGNORE_RETVAL(ComboBox_SetItemData(hTargetSystem,
ComboBox_AddStringU(hTargetSystem, lmprintf(MSG_033)), TT_BIOS));
IGNORE_RETVAL(ComboBox_SetCurSel(hTargetSystem, 0));
target_type = (int)ComboBox_GetCurItemData(hTargetSystem);
// Can't update a tooltip from a thread, so we send a message instead
SendMessage(hMainDialog, UM_UPDATE_CSM_TOOLTIP, 0, 0);
}
// Populate the Allocation unit size field
static BOOL SetClusterSizes(int FSType)
{
char* szClustSize;
int i, k, default_index = 0;
ULONG j;
IGNORE_RETVAL(ComboBox_ResetContent(hClusterSize));
if ((FSType < 0) || (FSType >= FS_MAX)) {
return FALSE;
}
if ((SelectedDrive.ClusterSize[FSType].Allowed == 0)
|| (SelectedDrive.ClusterSize[FSType].Default == 0)) {
return FALSE;
}
for (i = 0, j = 0x100, k = 0; j < 0x10000000; i++, j <<= 1) {
if (j & SelectedDrive.ClusterSize[FSType].Allowed) {
if (j == SelectedDrive.ClusterSize[FSType].Default) {
szClustSize = lmprintf(MSG_030, ClusterSizeLabel[i]);
default_index = k;
} else {
szClustSize = ClusterSizeLabel[i];
}
IGNORE_RETVAL(ComboBox_SetItemData(hClusterSize, ComboBox_AddStringU(hClusterSize, szClustSize), j));
k++;
}
}
IGNORE_RETVAL(ComboBox_SetCurSel(hClusterSize, default_index));
return TRUE;
}
static BOOL IsRefsAvailable(MEDIA_TYPE MediaType)
{
// The creation of ReFS drives was added in Windows 8.1... but then removed by
// Microsoft in Windows 10 1709, except for the Enterprise and Pro Workstation
// versions. Oh and VdsService::QueryFileSystemTypes() is *USELESS* to detect
// if ReFS is available on the system. Oh, and it only applies to fixed media.
if (MediaType != FixedMedia)
return FALSE;
if (WindowsVersion.Version < WINDOWS_8_1 || WindowsVersion.BuildNumber <= 0)
return FALSE;
// Per https://gist.github.com/0xbadfca11/da0598e47dd643d933dc
if (WindowsVersion.BuildNumber < 16226)
return TRUE;
switch (WindowsVersion.Edition) {
case 0x0000000A: // Enterprise Server
case 0x0000001B: // Enterprise N
case 0x00000046: // Enterprise E
case 0x00000048: // Enterprise Eval
case 0x00000054: // Enterprise N Eval
case 0x0000007D: // Enterprise S
case 0x0000007E: // Enterprise S N
case 0x00000081: // Enterprise S Eval
case 0x00000082: // Enterprise S N Eval
case 0x0000008C: // Enterprise Subscription
case 0x0000008D: // Enterprise Subscription N
case 0x000000A1: // Pro Workstation
case 0x000000A2: // Pro Workstation N
case 0x000000AB: // Enterprise G
case 0x000000AC: // Enterprise G N
return TRUE;
default:
return FALSE;
}
}
// Populate the File System and Cluster Size dropdowns
static BOOL SetFileSystemAndClusterSize(char* fs_name)
{
int fs_index;
LONGLONG i;
char tmp[128] = "", *entry;
IGNORE_RETVAL(ComboBox_ResetContent(hFileSystem));
IGNORE_RETVAL(ComboBox_ResetContent(hClusterSize));
default_fs = FS_UNKNOWN;
memset(&SelectedDrive.ClusterSize, 0, sizeof(SelectedDrive.ClusterSize));
/*
* See https://support.microsoft.com/en-gb/help/140365/default-cluster-size-for-ntfs--fat--and-exfat
* The following are MS's allowed cluster sizes for FAT16 and FAT32:
*
* FAT16
* 31M : 512 - 4096
* 63M : 1024 - 8192
* 127M : 2048 - 16k
* 255M : 4096 - 32k
* 511M : 8192 - 64k
* 1023M: 16k - 64k
* 2047M: 32k - 64k
* 4095M: 64k
* 4GB+ : N/A
*
* FAT32
* 31M : N/A
* 63M : N/A (NB unlike MS, we're allowing 512-512 here)
* 127M : 512 - 1024
* 255M : 512 - 2048
* 511M : 512 - 4096
* 1023M: 512 - 8192
* 2047M: 512 - 16k
* 4095M: 1024 - 32k
* 7GB : 2048 - 64k
* 15GB : 4096 - 64k
* 31GB : 8192 - 64k This is as far as Microsoft's FormatEx goes...
* 63GB : 16k - 64k ...but we can go higher using fat32format from RidgeCrop.
* 2TB+ : N/A
*
*/
// FAT 16
if (SelectedDrive.DiskSize < 4 * GB) {
SelectedDrive.ClusterSize[FS_FAT16].Allowed = 0x00001E00;
for (i = 32; i <= 4096; i <<= 1) { // 8 MB -> 4 GB
if (SelectedDrive.DiskSize < i * MB) {
SelectedDrive.ClusterSize[FS_FAT16].Default = 16 * (ULONG)i;
break;
}
SelectedDrive.ClusterSize[FS_FAT16].Allowed <<= 1;
}
SelectedDrive.ClusterSize[FS_FAT16].Allowed &= 0x0001FE00;
}
// FAT 32
// > 32GB FAT32 is not supported by MS and FormatEx but is achieved using fat32format
// See: http://ridgecrop.co.uk/index.htm?fat32format.htm
// < 32 MB FAT32 is not allowed by FormatEx, so we don't bother
if ((SelectedDrive.DiskSize >= 32 * MB) && (SelectedDrive.DiskSize < MAX_FAT32_SIZE)) {
SelectedDrive.ClusterSize[FS_FAT32].Allowed = 0x000001F8;
for (i = 32; i <= (32 * 1024); i <<= 1) { // 32 MB -> 32 GB
if (SelectedDrive.DiskSize*1.0f < i * MB * FAT32_CLUSTER_THRESHOLD) { // MS
SelectedDrive.ClusterSize[FS_FAT32].Default = 8*(ULONG)i;
break;
}
SelectedDrive.ClusterSize[FS_FAT32].Allowed <<= 1;
}
SelectedDrive.ClusterSize[FS_FAT32].Allowed &= 0x0001FE00;
// Default cluster sizes in the 256MB to 32 GB range do not follow the rule above
if ((SelectedDrive.DiskSize >= 256 * MB) && (SelectedDrive.DiskSize < 32 * GB)) {
for (i = 8; i <= 32; i <<= 1) { // 256 MB -> 32 GB
if (SelectedDrive.DiskSize * 1.0f < i * GB * FAT32_CLUSTER_THRESHOLD) {
SelectedDrive.ClusterSize[FS_FAT32].Default = ((ULONG)i / 2) * KB;
break;
}
}
}
// More adjustments for large drives
if (SelectedDrive.DiskSize >= 32 * GB) {
SelectedDrive.ClusterSize[FS_FAT32].Allowed &= 0x0001C000;
SelectedDrive.ClusterSize[FS_FAT32].Default = 0x00008000;
}
}
if (SelectedDrive.DiskSize < 256 * TB) {
// NTFS
SelectedDrive.ClusterSize[FS_NTFS].Allowed = 0x0001FE00;
for (i = 16; i <= 256; i <<= 1) { // 7 MB -> 256 TB
if (SelectedDrive.DiskSize < i * TB) {
SelectedDrive.ClusterSize[FS_NTFS].Default = ((ULONG)i / 4) * KB;
break;
}
}
// exFAT
SelectedDrive.ClusterSize[FS_EXFAT].Allowed = 0x03FFFE00;
if (SelectedDrive.DiskSize < 256 * MB) // < 256 MB
SelectedDrive.ClusterSize[FS_EXFAT].Default = 4 * KB;
else if (SelectedDrive.DiskSize < 32 * GB) // < 32 GB
SelectedDrive.ClusterSize[FS_EXFAT].Default = 32 * KB;
else
SelectedDrive.ClusterSize[FS_EXFAT].Default = 128 * KB;
// UDF
SelectedDrive.ClusterSize[FS_UDF].Allowed = SINGLE_CLUSTERSIZE_DEFAULT;
SelectedDrive.ClusterSize[FS_UDF].Default = 1;
// ext2/ext3/ext4
if (advanced_mode_format && (SelectedDrive.DiskSize >= MIN_EXT_SIZE)) {
SelectedDrive.ClusterSize[FS_EXT2].Allowed = SINGLE_CLUSTERSIZE_DEFAULT;
SelectedDrive.ClusterSize[FS_EXT2].Default = 1;
SelectedDrive.ClusterSize[FS_EXT3].Allowed = SINGLE_CLUSTERSIZE_DEFAULT;
SelectedDrive.ClusterSize[FS_EXT3].Default = 1;
}
// ReFS (only applicable for a select number of Windows platforms and editions)
if ((SelectedDrive.DiskSize >= 512 * MB) && (IsRefsAvailable(SelectedDrive.MediaType))) {
if (SelectedDrive.DiskSize < 16 * TB) { // < 16 TB
SelectedDrive.ClusterSize[FS_REFS].Allowed = 64 * KB + 4 * KB;
SelectedDrive.ClusterSize[FS_REFS].Default = 4 * KB;
} else {
SelectedDrive.ClusterSize[FS_REFS].Allowed = 64 * KB;
SelectedDrive.ClusterSize[FS_REFS].Default = 64 * KB;
}
}
}
// Only add the filesystems we can service
SetAllowedFileSystems();
SetClusterSizeLabels();
for (fs_index = 0; fs_index < FS_MAX; fs_index++) {
// Remove all cluster sizes that are below the sector size
if (SelectedDrive.ClusterSize[fs_index].Allowed != SINGLE_CLUSTERSIZE_DEFAULT) {
SelectedDrive.ClusterSize[fs_index].Allowed &= ~(SelectedDrive.SectorSize - 1);
if ((SelectedDrive.ClusterSize[fs_index].Default & SelectedDrive.ClusterSize[fs_index].Allowed) == 0)
// We lost our default => Use rightmost bit to select the new one
SelectedDrive.ClusterSize[fs_index].Default =
SelectedDrive.ClusterSize[fs_index].Allowed & (-(LONG)SelectedDrive.ClusterSize[fs_index].Allowed);
}
if (SelectedDrive.ClusterSize[fs_index].Allowed != 0) {
tmp[0] = 0;
// Tell the user if we're going to use Large FAT32 or regular
if ((fs_index == FS_FAT32) && ((SelectedDrive.DiskSize > LARGE_FAT32_SIZE) || (force_large_fat32)))
static_strcat(tmp, "Large ");
static_strcat(tmp, FileSystemLabel[fs_index]);
if (default_fs == FS_UNKNOWN) {
entry = lmprintf(MSG_030, tmp);
default_fs = fs_index;
} else {
entry = tmp;
}
if (allowed_filesystem[fs_index]) {
IGNORE_RETVAL(ComboBox_SetItemData(hFileSystem,
ComboBox_AddStringU(hFileSystem, entry), fs_index));
}
}
}
// re-select existing FS if it's one we know
SelectedDrive.FSType = FS_UNKNOWN;
if (safe_strlen(fs_name) != 0) {
for (SelectedDrive.FSType = FS_MAX - 1; SelectedDrive.FSType >= 0; SelectedDrive.FSType--) {
if (safe_strcmp(fs_name, FileSystemLabel[SelectedDrive.FSType]) == 0) {
break;
}
}
} else {
// Re-select last user-selected FS
SelectedDrive.FSType = selected_fs;
}
for (i = 0; i < ComboBox_GetCount(hFileSystem); i++) {
if (ComboBox_GetItemData(hFileSystem, i) == SelectedDrive.FSType) {
IGNORE_RETVAL(ComboBox_SetCurSel(hFileSystem, i));
break;
}
}
if (i == ComboBox_GetCount(hFileSystem)) {
// failed to reselect => pick default
SetComboEntry(hFileSystem, default_fs);
}
return SetClusterSizes((int)ComboBox_GetCurItemData(hFileSystem));
}
static void SetFSFromISO(void)
{
int i, fs_tmp, preferred_fs = FS_UNKNOWN;
uint32_t fs_mask = FS_NTFS | (img_report.has_4GB_file ? 0 : FS_FAT32);
BOOL windows_to_go = (image_options & IMOP_WINTOGO) && (boot_type == BT_IMAGE) &&
HAS_WINTOGO(img_report) && (ComboBox_GetCurItemData(hImageOption) == IMOP_WIN_TO_GO);
if (image_path == NULL)
return;
// Create a mask of all the FS's available
for (i = 0; i < ComboBox_GetCount(hFileSystem); i++) {
fs_tmp = (int)ComboBox_GetItemData(hFileSystem, i);
fs_mask |= 1 << fs_tmp;
}
// If the FS requested from the command line is valid use it
if ((preselected_fs != FS_UNKNOWN) && (fs_mask & (1 << preselected_fs))) {
preferred_fs = preselected_fs;
} else {
// Syslinux and EFI have precedence over bootmgr (unless the user selected BIOS as target type)
if ((HAS_SYSLINUX(img_report)) || (HAS_REACTOS(img_report)) || HAS_KOLIBRIOS(img_report) ||
(IS_EFI_BOOTABLE(img_report) && (target_type == TT_UEFI) && (!windows_to_go) && (!img_report.has_4GB_file))) {
if (fs_mask & (1 << FS_FAT32))
preferred_fs = FS_FAT32;
else if ((fs_mask & (1 << FS_FAT16)) && !HAS_KOLIBRIOS(img_report))
preferred_fs = FS_FAT16;
} else if ((windows_to_go) || HAS_BOOTMGR(img_report) || HAS_WINPE(img_report)) {
if ((fs_mask & (1 << FS_FAT32)) && (!img_report.has_4GB_file) && (allow_dual_uefi_bios))
preferred_fs = FS_FAT32;
else if (fs_mask & (1 << FS_NTFS))
preferred_fs = FS_NTFS;
}
}
// Try to select the FS
for (i = 0; i < ComboBox_GetCount(hFileSystem); i++) {
fs_tmp = (int)ComboBox_GetItemData(hFileSystem, i);
if (fs_tmp == preferred_fs) {
IGNORE_RETVAL(ComboBox_SetCurSel(hFileSystem, i));
break;
}
}
if (selected_fs == FS_UNKNOWN)
selected_fs = preferred_fs;
SendMessage(hMainDialog, WM_COMMAND, (CBN_SELCHANGE_INTERNAL << 16) | IDC_FILE_SYSTEM,
ComboBox_GetCurSel(hFileSystem));
}
static void SetProposedLabel(int ComboIndex)
{
const char no_label[] = STR_NO_LABEL, empty[] = "";
// If the user manually changed the label, try to preserve it
if (user_changed_label)
return;
app_changed_label = TRUE;
// If bootable ISO creation is selected, and we have an ISO selected with a valid name, use that
// Also some distros (eg. Arch) require the USB to have the same label as the ISO
if ((boot_type == BT_IMAGE) && (image_path != NULL) && (img_report.label[0] != 0)) {
SetWindowTextU(hLabel, img_report.label);
return;
}
// Empty the label if no device is currently selected
if (ComboIndex < 0) {
SetWindowTextU(hLabel, "");
return;
}
// Else if no existing label is available, propose one according to the size (eg: "256MB", "8GB")
if ((_stricmp(no_label, rufus_drive[ComboIndex].label) == 0) || (_stricmp(no_label, empty) == 0)
|| (safe_stricmp(lmprintf(MSG_207), rufus_drive[ComboIndex].label) == 0)) {
SetWindowTextU(hLabel, SelectedDrive.proposed_label);
} else {
SetWindowTextU(hLabel, rufus_drive[ComboIndex].label);
}
}
static void EnableOldBiosFixes(BOOL enable, BOOL remove_checkboxes)
{
static UINT checked, state = 0;
HWND hCtrl = GetDlgItem(hMainDialog, IDC_OLD_BIOS_FIXES);
// The fix for old BIOSes option cannot apply if we aren't targetting BIOS, or are using an image that isn't BIOS bootable
if ((partition_type != PARTITION_STYLE_MBR) || (target_type != TT_BIOS) || (boot_type == BT_NON_BOOTABLE) ||
((boot_type == BT_IMAGE) && (!IS_BIOS_BOOTABLE(img_report) || IS_DD_ONLY(img_report)))) {
enable = FALSE;
}
if (remove_checkboxes) {
if (!enable && (state != 1)) {
checked = IsChecked(IDC_OLD_BIOS_FIXES);
CheckDlgButton(hMainDialog, IDC_OLD_BIOS_FIXES, BST_UNCHECKED);
state = 1;
} else if (enable && !IsWindowEnabled(hCtrl) && (state != 2)) {
if (state != 0)
CheckDlgButton(hMainDialog, IDC_OLD_BIOS_FIXES, checked);
state = 2;
}
}
EnableWindow(hCtrl, enable);
}
static void EnableUefiValidation(BOOL enable, BOOL remove_checkboxes)
{
UINT checked = validate_md5sum ? BST_CHECKED : BST_UNCHECKED;
HWND hCtrl = GetDlgItem(hMainDialog, IDC_UEFI_MEDIA_VALIDATION);
// The UEFI validation bootloader cannot apply if we don't write an ISO, or if the ISO is not UEFI bootable
// or if it's a Windows To Go installation or if DD or BIOS/CSM only are enforced.
if ((boot_type != BT_IMAGE) || (!IS_EFI_BOOTABLE(img_report)) || IS_DD_ONLY(img_report) ||
((image_options & IMOP_WINTOGO) && (ComboBox_GetCurItemData(hImageOption) == IMOP_WIN_TO_GO)) ||
((target_type == TT_BIOS) && HAS_WINDOWS(img_report) && (!allow_dual_uefi_bios))) {
enable = FALSE;
}
if (!enable && remove_checkboxes)
CheckDlgButton(hMainDialog, IDC_UEFI_MEDIA_VALIDATION, BST_UNCHECKED);
else
CheckDlgButton(hMainDialog, IDC_UEFI_MEDIA_VALIDATION, checked);
EnableWindow(hCtrl, enable);
}
static void EnableExtendedLabel(BOOL enable, BOOL remove_checkboxes)
{
static UINT checked, state = 0;
HWND hCtrl = GetDlgItem(hMainDialog, IDC_EXTENDED_LABEL);
if (IS_EXT(fs_type) || ((boot_type == BT_IMAGE) && IS_DD_ONLY(img_report)))
enable = FALSE;
if (remove_checkboxes) {
if (!enable && (state != 1)) {
checked = IsChecked(IDC_EXTENDED_LABEL);
CheckDlgButton(hMainDialog, IDC_EXTENDED_LABEL, BST_UNCHECKED);
state = 1;
} else if (enable && !IsWindowEnabled(hCtrl) && (state != 2)) {
if (state != 0)
CheckDlgButton(hMainDialog, IDC_EXTENDED_LABEL, checked);
state = 2;
}
}
EnableWindow(hCtrl, enable);
}
static void EnableQuickFormat(BOOL enable, BOOL remove_checkboxes)
{
static UINT checked, state = 0;
HWND hCtrl = GetDlgItem(hMainDialog, IDC_QUICK_FORMAT);
if ((boot_type == BT_IMAGE) && IS_DD_ONLY(img_report))
enable = FALSE;
// Disable/restore the quick format control depending on large FAT32 or ReFS
if (((fs_type == FS_FAT32) && ((SelectedDrive.DiskSize > LARGE_FAT32_SIZE) || (force_large_fat32))) || (fs_type == FS_REFS)) {
enable = FALSE;
// Quick Format is the only option for the above
remove_checkboxes = FALSE;
CheckDlgButton(hMainDialog, IDC_QUICK_FORMAT, BST_CHECKED);
}
if (remove_checkboxes) {
if (!enable && (state != 1)) {
checked = IsChecked(IDC_QUICK_FORMAT);
CheckDlgButton(hMainDialog, IDC_QUICK_FORMAT, BST_UNCHECKED);
state = 1;
} else if (enable && !IsWindowEnabled(hCtrl) && (state != 2)) {
if (state != 0)
CheckDlgButton(hMainDialog, IDC_QUICK_FORMAT, checked);
state = 2;
}
}
EnableWindow(hCtrl, enable);
}
static void EnableBootOptions(BOOL enable, BOOL remove_checkboxes)
{
BOOL actual_enable_bb, actual_enable = enable;
// If no device is selected, don't enable anything and also don't remove the checkboxes
if (ComboBox_GetCurSel(hDeviceList) < 0) {
actual_enable = FALSE;
remove_checkboxes = FALSE;
}
// If boot selection is set to image, but no image is currently selected, don't enable anything
if ((boot_type == BT_IMAGE) && (image_path == NULL))
actual_enable = FALSE;
actual_enable_bb = actual_enable;
// If we are dealing with a pure DD image, remove all options except Bad Blocks check
if ((boot_type == BT_IMAGE) && IS_DD_BOOTABLE(img_report) && (!img_report.is_iso))
actual_enable = FALSE;
EnableWindow(hImageOption, actual_enable);
EnableWindow(GetDlgItem(hMainDialog, IDC_PERSISTENCE_SLIDER), actual_enable);
// Make sure we set the range if we have persistence
if ((image_path != NULL) && HAS_PERSISTENCE(img_report))
SetPersistenceSize();
EnableWindow(GetDlgItem(hMainDialog, IDC_PERSISTENCE_SIZE), (persistence_size != 0) && actual_enable);
EnableWindow(GetDlgItem(hMainDialog, IDC_PERSISTENCE_UNITS), (persistence_size != 0) && actual_enable);
EnableOldBiosFixes(actual_enable, remove_checkboxes);
EnableUefiValidation(actual_enable, remove_checkboxes);
EnableWindow(GetDlgItem(hMainDialog, IDC_LABEL), actual_enable);
if (boot_type == BT_IMAGE) {
if (IS_DD_ONLY(img_report))
remove_checkboxes = TRUE;
else if (image_path == NULL)
remove_checkboxes = FALSE;
}
EnableQuickFormat(actual_enable, remove_checkboxes);
EnableExtendedLabel(actual_enable, remove_checkboxes);
EnableWindow(GetDlgItem(hMainDialog, IDC_BAD_BLOCKS), actual_enable_bb);
EnableWindow(GetDlgItem(hMainDialog, IDC_NB_PASSES), actual_enable_bb);
}
// Toggle controls according to operation
void EnableControls(BOOL enable, BOOL remove_checkboxes)
{
op_in_progress = !enable;
// The following only get disabled on format/hash and otherwise remain enabled,
// even if no device or image are selected
EnableWindow(hDeviceList, enable);
EnableWindow(hBootType, enable);
EnableWindow(hSelectImage, enable);
EnableWindow(GetDlgItem(hMainDialog, IDC_LIST_USB_HDD), enable);
EnableWindow(hAdvancedDeviceToolbar, enable);
EnableWindow(hAdvancedFormatToolbar, enable);
SendMessage(hMultiToolbar, TB_ENABLEBUTTON, (WPARAM)IDC_LANG, (LPARAM)enable);
SendMessage(hMultiToolbar, TB_ENABLEBUTTON, (WPARAM)IDC_ABOUT, (LPARAM)enable);
SendMessage(hMultiToolbar, TB_ENABLEBUTTON, (WPARAM)IDC_SETTINGS, (LPARAM)enable);
// Hash button is enabled if an image has been selected
EnableWindow(hHashToolbar, enable && (boot_type == BT_IMAGE) && (image_path != NULL));
// Toggle CLOSE/CANCEL
SetDlgItemTextU(hMainDialog, IDCANCEL, enable ? uppercase_close : uppercase_cancel);
// Only enable the following controls if a device is active
enable = (ComboBox_GetCurSel(hDeviceList) < 0) ? FALSE : enable;
EnableWindow(hImageOption, enable);
EnableWindow(hSaveToolbar, enable);
// Enable or disable the Start button and the other boot options
enable = ((boot_type == BT_IMAGE) && (image_path == NULL)) ? FALSE : enable;
EnableWindow(hStart, enable);
EnableBootOptions(enable, remove_checkboxes);
// Finally, only enable the half-size dropdowns if we aren't dealing with a pure DD image
enable = ((boot_type == BT_IMAGE) && (image_path != NULL) &&
(!(img_report.is_iso || img_report.is_windows_img))) ? FALSE : enable;
EnableWindow(hPartitionScheme, enable);
EnableWindow(hTargetSystem, enable);
EnableWindow(GetDlgItem(hMainDialog, IDS_CSM_HELP_TXT), enable);
EnableWindow(hFileSystem, enable);
EnableWindow(hClusterSize, enable);
}
// Populate the UI main dropdown properties.
// This should be called on device or boot type change.
static BOOL PopulateProperties(void)
{
char* device_tooltip;
int device_index = ComboBox_GetCurSel(hDeviceList);
char fs_name[32];
memset(&SelectedDrive, 0, sizeof(SelectedDrive));
EnableWindow(hStart, FALSE);
if (device_index < 0)
goto out;
persistence_unit_selection = -1;
// Get data from the currently selected drive
SelectedDrive.DeviceNumber = (DWORD)ComboBox_GetItemData(hDeviceList, device_index);
// This fills the SelectedDrive properties
GetDrivePartitionData(SelectedDrive.DeviceNumber, fs_name, sizeof(fs_name), FALSE);
SetPartitionSchemeAndTargetSystem(FALSE);
// Attempt to reselect the last file system explicitly set by the user
if (!SetFileSystemAndClusterSize((selected_fs == FS_UNKNOWN) ? fs_name : NULL)) {
SetProposedLabel(-1);
uprintf("No file system is selectable for this drive\n");
return FALSE;
}
EnableControls(TRUE, FALSE);
// Set a proposed label according to the size (eg: "256MB", "8GB")
static_sprintf(SelectedDrive.proposed_label, "%s",
SizeToHumanReadable(SelectedDrive.DiskSize, FALSE, TRUE));
// Add a tooltip (with the size of the device in parenthesis)
device_tooltip = (char*) malloc(safe_strlen(rufus_drive[device_index].name) + 32);
if (device_tooltip != NULL) {
if (right_to_left_mode)
safe_sprintf(device_tooltip, safe_strlen(rufus_drive[device_index].name) + 32, "(%s) %s",
SizeToHumanReadable(SelectedDrive.DiskSize, FALSE, FALSE), rufus_drive[device_index].name);
else
safe_sprintf(device_tooltip, safe_strlen(rufus_drive[device_index].name) + 32, "%s (%s)",
rufus_drive[device_index].name, SizeToHumanReadable(SelectedDrive.DiskSize, FALSE, FALSE));
CreateTooltip(hDeviceList, device_tooltip, -1);
free(device_tooltip);
}
out:
SetProposedLabel(device_index);
return TRUE;
}
// Callback for the log window
BOOL CALLBACK LogCallback(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hDC;
HFONT hf;
LONG lfHeight;
LONG_PTR style;
DWORD log_size;
char *log_buffer = NULL, *filepath;
EXT_DECL(log_ext, "rufus.log", __VA_GROUP__("*.log"), __VA_GROUP__("Rufus log"));
switch (message) {
case WM_INITDIALOG:
apply_localization(IDD_LOG, hDlg);
hLog = GetDlgItem(hDlg, IDC_LOG_EDIT);
// Increase the size of our log textbox to MAX_LOG_SIZE (unsigned word)
PostMessage(hLog, EM_LIMITTEXT, MAX_LOG_SIZE , 0);
// Set the font to Unicode so that we can display anything
hDC = GetDC(NULL);
lfHeight = -MulDiv(9, GetDeviceCaps(hDC, LOGPIXELSY), 72);
safe_release_dc(NULL, hDC);
hf = CreateFontA(lfHeight, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE,
DEFAULT_CHARSET, 0, 0, PROOF_QUALITY, 0, "Consolas");
SendDlgItemMessageA(hDlg, IDC_LOG_EDIT, WM_SETFONT, (WPARAM)hf, TRUE);
// Set 'Close Log' as the selected button
SendMessage(hDlg, WM_NEXTDLGCTL, (WPARAM)GetDlgItem(hDlg, IDCANCEL), TRUE);
// Suppress any inherited RTL flags from our edit control's style. Otherwise
// the displayed text becomes a mess due to Windows trying to interpret
// dots, parenthesis, columns and so on in an RTL context...
// We also take this opportunity to fix the scroll bar and text alignment.
style = GetWindowLongPtr(hLog, GWL_EXSTYLE);
style &= ~(WS_EX_RTLREADING | WS_EX_RIGHT | WS_EX_LEFTSCROLLBAR);
SetWindowLongPtr(hLog, GWL_EXSTYLE, style);
style = GetWindowLongPtr(hLog, GWL_STYLE);
style &= ~(ES_RIGHT);
SetWindowLongPtr(hLog, GWL_STYLE, style);
break;
case WM_COMMAND:
switch (LOWORD(wParam)) {
case IDCANCEL:
ShowWindow(hDlg, SW_HIDE);
log_displayed = FALSE;
// Set focus to the Cancel button on the main dialog
// This avoids intempestive tooltip display from the log toolbar button
SendMessage(hMainDialog, WM_NEXTDLGCTL, (WPARAM)GetDlgItem(hMainDialog, IDCANCEL), TRUE);
return TRUE;
case IDC_LOG_CLEAR:
SetWindowTextA(hLog, "");
return TRUE;
case IDC_LOG_SAVE:
log_size = GetWindowTextLengthU(hLog);
if (log_size <= 0)
break;
log_buffer = (char*)malloc(log_size);
if (log_buffer != NULL) {
log_size = GetDlgItemTextU(hDlg, IDC_LOG_EDIT, log_buffer, log_size);
if (log_size != 0) {
log_size--; // remove NUL terminator
filepath = FileDialog(TRUE, user_dir, &log_ext, NULL);
if (filepath != NULL)