11// SPDX-License-Identifier: GPL-2.0-only
22
33use std:: fs:: read_dir;
4- use std:: os:: unix:: ffi:: OsStrExt ;
54use std:: os:: unix:: fs:: symlink;
6- use std:: { thread, time} ;
5+ use std:: thread;
6+ use std:: time:: Duration ;
77
88use log:: debug;
99
@@ -12,15 +12,21 @@ use crate::mount::mount_apivfs;
1212use crate :: util:: { mkdir, write_file} ;
1313use crate :: Result ;
1414
15- fn setup_9pfs_gadget ( device : & String ) -> Result < ( ) > {
15+ fn setup_9pfs_gadget ( device : Option < & str > ) -> Result < ( ) > {
1616 debug ! ( "Initializing USB 9pfs gadget ..." ) ;
1717
18- let udc = read_dir ( "/sys/class/udc" )
19- . map_err ( |e| format ! ( "Failed to list /sys/class/udc: {e}" ) ) ?
20- . next ( )
21- . ok_or ( "No UDC found to attach the 9pfs gadget" . to_string ( ) ) ?
22- . map_err ( |e| format ! ( "Failed to inspect the first entry in /sys/class/udc: {e}" ) ) ?
23- . file_name ( ) ;
18+ let udc = if let Some ( device) = device {
19+ device. to_owned ( )
20+ } else {
21+ read_dir ( "/sys/class/udc" )
22+ . map_err ( |e| format ! ( "Failed to list /sys/class/udc: {e}" ) ) ?
23+ . next ( )
24+ . ok_or ( "No UDC found to attach the 9pfs gadget" . to_string ( ) ) ?
25+ . map_err ( |e| format ! ( "Failed to inspect the first entry in /sys/class/udc: {e}" ) ) ?
26+ . file_name ( )
27+ . into_string ( )
28+ . map_err ( |e| format ! ( "invalid utf-8 in file name: {e:?}" ) ) ?
29+ } ;
2430
2531 mount_apivfs ( "/sys/kernel/config" , "configfs" ) ?;
2632
@@ -46,22 +52,17 @@ fn setup_9pfs_gadget(device: &String) -> Result<()> {
4652 mkdir ( "/sys/kernel/config/usb_gadget/9pfs/configs/c.1" ) ?;
4753 mkdir ( "/sys/kernel/config/usb_gadget/9pfs/configs/c.1/strings/0x409" ) ?;
4854
49- let function = format ! ( "/sys/kernel/config/usb_gadget/9pfs/functions/usb9pfs.{device} " ) ;
50- let link = format ! ( "/sys/kernel/config/usb_gadget/9pfs/configs/c.1/usb9pfs.{device} " ) ;
55+ let function = format ! ( "/sys/kernel/config/usb_gadget/9pfs/functions/usb9pfs.rootfs " ) ;
56+ let link = format ! ( "/sys/kernel/config/usb_gadget/9pfs/configs/c.1/usb9pfs.rootfs " ) ;
5157 mkdir ( & function) ?;
5258 symlink ( & function, & link) ?;
5359
54- debug ! (
55- "Attaching 9pfs gatget to UDC {}" ,
56- udc. as_bytes( ) . escape_ascii( )
57- ) ;
58- write_file (
59- "/sys/kernel/config/usb_gadget/9pfs/UDC" ,
60- udc. as_encoded_bytes ( ) ,
61- ) ?;
60+ let udc = udc. as_bytes ( ) . escape_ascii ( ) . to_string ( ) ;
61+
62+ debug ! ( "Attaching 9pfs gatget to UDC {udc}" , ) ;
63+ write_file ( "/sys/kernel/config/usb_gadget/9pfs/UDC" , udc) ?;
6264
63- let d = time:: Duration :: new ( 1 , 0 ) ;
64- thread:: sleep ( d) ;
65+ thread:: sleep ( Duration :: from_secs ( 1 ) ) ;
6566 Ok ( ( ) )
6667}
6768
@@ -72,12 +73,8 @@ pub fn prepare_9pfs_gadget(options: &CmdlineOptions) -> Result<bool> {
7273 . as_deref ( )
7374 . is_some_and ( |flags| flags. contains ( "trans=usbg" ) )
7475 {
75- if let Some ( root) = & options. root {
76- setup_9pfs_gadget ( root) ?;
77- Ok ( true )
78- } else {
79- Err ( "Missing root= for 9p!" . into ( ) )
80- }
76+ setup_9pfs_gadget ( options. root . as_deref ( ) ) ?;
77+ Ok ( true )
8178 } else {
8279 Ok ( false )
8380 }
0 commit comments