Skip to content

Commit 66c12cc

Browse files
committed
Updating extern functions to be FFI safe
1 parent 6b99fb4 commit 66c12cc

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

uefi/src/proto/shell/mod.rs

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ use super::media::file::FileInfo;
1818
pub struct Shell {
1919
execute: extern "efiapi" fn(
2020
parent_image_handle: *const Handle,
21-
commandline: *const CStr16,
22-
environment: *const *const CStr16,
21+
commandline: *const Char16,
22+
environment: *const *const Char16,
2323
out_status: *mut Status,
2424
) -> Status,
2525
get_env: extern "efiapi" fn(name: *const Char16) -> *const Char16,
@@ -52,19 +52,19 @@ pub struct Shell {
5252
open_file_by_name: usize,
5353
close_file: extern "efiapi" fn(file_handle: ShellFileHandle) -> Status,
5454
create_file: extern "efiapi" fn(
55-
file_name: &CStr16,
55+
file_name: *const Char16,
5656
file_attribs: u64,
5757
out_file_handle: ShellFileHandle,
5858
) -> Status,
5959
read_file: usize,
6060
write_file: usize,
6161
delete_file: extern "efiapi" fn(file_handle: ShellFileHandle) -> Status,
62-
delete_file_by_name: extern "efiapi" fn(file_name: &CStr16) -> Status,
62+
delete_file_by_name: extern "efiapi" fn(file_name: *const Char16) -> Status,
6363
get_file_position: usize,
6464
set_file_position: usize,
6565
flush_file: extern "efiapi" fn(file_handle: ShellFileHandle) -> Status,
6666
find_files: extern "efiapi" fn(
67-
file_pattern: *const CStr16,
67+
file_pattern: *const Char16,
6868
out_file_list: *mut *mut ShellFileInfo,
6969
) -> Status,
7070
find_files_in_dir: extern "efiapi" fn(
@@ -132,16 +132,16 @@ impl Shell {
132132
) -> Result<Status> {
133133
let mut out_status: MaybeUninit<Status> = MaybeUninit::uninit();
134134
// We have to do this in two parts, an `as` cast straight to *const *const CStr16 doesn't compile
135-
let environment = environment.as_ptr();
136-
let environment = environment.cast::<*const CStr16>();
137-
138-
(self.execute)(
139-
&parent_image,
140-
command_line,
141-
environment,
142-
out_status.as_mut_ptr(),
143-
)
144-
.to_result_with_val(|| unsafe { out_status.assume_init() })
135+
// let environment = environment.as_ptr();
136+
// let environment = environment.cast::<*const CStr16>();
137+
138+
let cl_ptr = command_line.as_ptr();
139+
unsafe {
140+
let env_ptr: *const *const Char16 = (&(*environment.as_ptr()).as_ptr()).cast();
141+
142+
(self.execute)(&parent_image, cl_ptr, env_ptr, out_status.as_mut_ptr())
143+
.to_result_with_val(|| out_status.assume_init())
144+
}
145145
}
146146

147147
/// Gets the environment variable or list of environment variables
@@ -300,8 +300,10 @@ impl Shell {
300300
//let mut out_file_handle: MaybeUninit<Option<ShellFileHandle>> = MaybeUninit::zeroed();
301301
// let mut file_handle: ShellFileHandle;
302302
let file_handle = ptr::null();
303+
let file_name_ptr = file_name.as_ptr();
303304

304-
(self.create_file)(file_name, file_attribs, file_handle).to_result_with_val(|| file_handle)
305+
(self.create_file)(file_name_ptr, file_attribs, file_handle)
306+
.to_result_with_val(|| file_handle)
305307
// Safety: if this call is successful, `out_file_handle`
306308
// will always be initialized and valid.
307309
// .to_result_with_val(|| unsafe { out_file_handle.assume_init() })
@@ -314,7 +316,7 @@ impl Shell {
314316

315317
/// TODO
316318
pub fn delete_file_by_name(&self, file_name: &CStr16) -> Result<()> {
317-
(self.delete_file_by_name)(file_name).to_result()
319+
(self.delete_file_by_name)(file_name.as_ptr()).to_result()
318320
}
319321

320322
/// TODO
@@ -324,7 +326,8 @@ impl Shell {
324326
if out_ptr.is_null() {
325327
panic!("outptr null");
326328
}
327-
(self.find_files)(file_pattern, out_ptr).to_result_with_val(|| {
329+
let fp_ptr = file_pattern.as_ptr();
330+
(self.find_files)(fp_ptr, out_ptr).to_result_with_val(|| {
328331
// safety: if we're here, out_list is valid, but maybe null
329332
let out_list = unsafe { out_list.assume_init() };
330333
if out_list.is_null() {

0 commit comments

Comments
 (0)