1
1
// SPDX-License-Identifier: GPL-2.0-only
2
2
3
3
use std:: fs:: read_dir;
4
- use std:: os:: unix:: ffi:: OsStrExt ;
5
4
use std:: os:: unix:: fs:: symlink;
6
- use std:: { thread, time} ;
5
+ use std:: thread;
6
+ use std:: time:: Duration ;
7
7
8
8
use log:: debug;
9
9
@@ -12,15 +12,24 @@ use crate::mount::mount_apivfs;
12
12
use crate :: util:: { mkdir, write_file} ;
13
13
use crate :: Result ;
14
14
15
- fn setup_9pfs_gadget ( device : & String ) -> Result < ( ) > {
15
+ fn setup_9pfs_gadget ( options : & mut CmdlineOptions ) -> Result < ( ) > {
16
16
debug ! ( "Initializing USB 9pfs gadget ..." ) ;
17
17
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) = & options. root {
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
+ }
30
+ . as_bytes ( )
31
+ . escape_ascii ( )
32
+ . to_string ( ) ;
24
33
25
34
mount_apivfs ( "/sys/kernel/config" , "configfs" ) ?;
26
35
@@ -46,38 +55,30 @@ fn setup_9pfs_gadget(device: &String) -> Result<()> {
46
55
mkdir ( "/sys/kernel/config/usb_gadget/9pfs/configs/c.1" ) ?;
47
56
mkdir ( "/sys/kernel/config/usb_gadget/9pfs/configs/c.1/strings/0x409" ) ?;
48
57
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 }" ) ;
58
+ let function = format ! ( "/sys/kernel/config/usb_gadget/9pfs/functions/usb9pfs.{udc }" ) ;
59
+ let link = format ! ( "/sys/kernel/config/usb_gadget/9pfs/configs/c.1/usb9pfs.{udc }" ) ;
51
60
mkdir ( & function) ?;
52
61
symlink ( & function, & link) ?;
53
62
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
- ) ?;
63
+ debug ! ( "Attaching 9pfs gatget to UDC {udc}" , ) ;
64
+ write_file ( "/sys/kernel/config/usb_gadget/9pfs/UDC" , & udc) ?;
65
+
66
+ thread:: sleep ( Duration :: from_secs ( 1 ) ) ;
67
+
68
+ options. root = Some ( udc) ;
62
69
63
- let d = time:: Duration :: new ( 1 , 0 ) ;
64
- thread:: sleep ( d) ;
65
70
Ok ( ( ) )
66
71
}
67
72
68
- pub fn prepare_9pfs_gadget ( options : & CmdlineOptions ) -> Result < bool > {
73
+ pub fn prepare_9pfs_gadget ( options : & mut CmdlineOptions ) -> Result < bool > {
69
74
if options. rootfstype . as_deref ( ) == Some ( "9p" )
70
75
&& options
71
76
. rootflags
72
77
. as_deref ( )
73
78
. is_some_and ( |flags| flags. contains ( "trans=usbg" ) )
74
79
{
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
- }
80
+ setup_9pfs_gadget ( options) ?;
81
+ Ok ( true )
81
82
} else {
82
83
Ok ( false )
83
84
}
0 commit comments