@@ -69,90 +69,34 @@ for all possible configuration options.
69
69
70
70
use anyhow:: Context ;
71
71
use std:: {
72
+ collections:: BTreeMap ,
72
73
fs:: { self , File } ,
73
74
io:: { self , Seek } ,
74
75
path:: Path ,
75
76
} ;
76
77
78
+ mod fat;
79
+
80
+ const KERNEL_FILE_NAME : & str = "kernel-x86_64" ;
81
+
77
82
pub fn create_uefi_disk_image (
78
83
kernel_binary : & Path ,
79
84
out_fat_path : & Path ,
80
85
out_gpt_path : & Path ,
81
86
) -> anyhow:: Result < ( ) > {
82
87
let bootloader_path = Path :: new ( env ! ( "UEFI_BOOTLOADER_PATH" ) ) ;
83
88
84
- create_fat_filesystem ( bootloader_path, kernel_binary, & out_fat_path)
89
+ let mut files = BTreeMap :: new ( ) ;
90
+ files. insert ( "efi/boot/bootx64.efi" , bootloader_path) ;
91
+ files. insert ( KERNEL_FILE_NAME , kernel_binary) ;
92
+
93
+ fat:: create_fat_filesystem ( files, & out_fat_path)
85
94
. context ( "failed to create UEFI FAT filesystem" ) ?;
86
95
create_gpt_disk ( out_fat_path, out_gpt_path) ;
87
96
88
97
Ok ( ( ) )
89
98
}
90
99
91
- fn create_fat_filesystem (
92
- bootloader_efi_file : & Path ,
93
- kernel_binary : & Path ,
94
- out_fat_path : & Path ,
95
- ) -> anyhow:: Result < ( ) > {
96
- const MB : u64 = 1024 * 1024 ;
97
-
98
- // retrieve size of `.efi` file and round it up
99
- let efi_size = fs:: metadata ( & bootloader_efi_file) . unwrap ( ) . len ( ) ;
100
- let kernel_size = fs:: metadata ( & kernel_binary)
101
- . context ( "failed to read metadata of kernel binary" ) ?
102
- . len ( ) ;
103
-
104
- // create new filesystem image file at the given path and set its length
105
- let fat_file = fs:: OpenOptions :: new ( )
106
- . read ( true )
107
- . write ( true )
108
- . create ( true )
109
- . truncate ( true )
110
- . open ( & out_fat_path)
111
- . unwrap ( ) ;
112
- let fat_size = efi_size + kernel_size;
113
- let fat_size_padded_and_rounded = ( ( fat_size + 1024 * 64 - 1 ) / MB + 1 ) * MB ;
114
- fat_file. set_len ( fat_size_padded_and_rounded) . unwrap ( ) ;
115
-
116
- // create new FAT file system and open it
117
- let label = {
118
- if let Some ( name) = bootloader_efi_file. file_stem ( ) {
119
- let converted = name. to_string_lossy ( ) ;
120
- let name = converted. as_bytes ( ) ;
121
- let mut label = [ 0u8 ; 11 ] ;
122
- let name = & name[ ..label. len ( ) ] ;
123
- let slice = & mut label[ ..name. len ( ) ] ;
124
- slice. copy_from_slice ( name) ;
125
- label
126
- } else {
127
- * b"MY_RUST_OS!"
128
- }
129
- } ;
130
- let format_options = fatfs:: FormatVolumeOptions :: new ( ) . volume_label ( label) ;
131
- fatfs:: format_volume ( & fat_file, format_options) . context ( "Failed to format UEFI FAT file" ) ?;
132
- let filesystem = fatfs:: FileSystem :: new ( & fat_file, fatfs:: FsOptions :: new ( ) )
133
- . context ( "Failed to open FAT file system of UEFI FAT file" ) ?;
134
-
135
- // copy EFI file to FAT filesystem
136
- let root_dir = filesystem. root_dir ( ) ;
137
- root_dir. create_dir ( "efi" ) . unwrap ( ) ;
138
- root_dir. create_dir ( "efi/boot" ) . unwrap ( ) ;
139
- let mut bootx64 = root_dir. create_file ( "efi/boot/bootx64.efi" ) . unwrap ( ) ;
140
- bootx64. truncate ( ) . unwrap ( ) ;
141
- io:: copy (
142
- & mut fs:: File :: open ( & bootloader_efi_file) . unwrap ( ) ,
143
- & mut bootx64,
144
- )
145
- . unwrap ( ) ;
146
-
147
- // copy kernel to FAT filesystem
148
- let mut kernel_file = root_dir. create_file ( "kernel-x86_64" ) ?;
149
- kernel_file. truncate ( ) ?;
150
- io:: copy ( & mut fs:: File :: open ( & kernel_binary) ?, & mut kernel_file)
151
- . context ( "failed to copy kernel to UEFI FAT filesystem" ) ?;
152
-
153
- Ok ( ( ) )
154
- }
155
-
156
100
fn create_gpt_disk ( fat_image : & Path , out_gpt_path : & Path ) {
157
101
// create new file
158
102
let mut disk = fs:: OpenOptions :: new ( )
0 commit comments