@@ -107,13 +107,23 @@ fn start_android_emulator(server: &Path) {
107107 Command :: new ( "adb" ) . arg ( "shell" ) . arg ( "/data/tmp/testd" ) . spawn ( ) . unwrap ( ) ;
108108}
109109
110- fn start_qemu_emulator ( target : & str , rootfs : & Path , server : & Path , tmpdir : & Path ) {
110+ fn prepare_rootfs ( target : & str , rootfs : & Path , server : & Path , rootfs_img : & Path ) {
111+ t ! ( fs:: copy( server, rootfs. join( "testd" ) ) ) ;
112+
113+ match target {
114+ "arm-unknown-linux-gnueabihf" | "aarch64-unknown-linux-gnu" => {
115+ prepare_rootfs_cpio ( rootfs, rootfs_img)
116+ }
117+ "riscv64gc-unknown-linux-gnu" => prepare_rootfs_ext4 ( rootfs, rootfs_img) ,
118+ _ => panic ! ( "{} is not supported" , target) ,
119+ }
120+ }
121+
122+ fn prepare_rootfs_cpio ( rootfs : & Path , rootfs_img : & Path ) {
111123 // Generate a new rootfs image now that we've updated the test server
112124 // executable. This is the equivalent of:
113125 //
114126 // find $rootfs -print 0 | cpio --null -o --format=newc > rootfs.img
115- t ! ( fs:: copy( server, rootfs. join( "testd" ) ) ) ;
116- let rootfs_img = tmpdir. join ( "rootfs.img" ) ;
117127 let mut cmd = Command :: new ( "cpio" ) ;
118128 cmd. arg ( "--null" )
119129 . arg ( "-o" )
@@ -128,6 +138,38 @@ fn start_qemu_emulator(target: &str, rootfs: &Path, server: &Path, tmpdir: &Path
128138 t ! ( io:: copy( & mut child. stdout. take( ) . unwrap( ) , & mut t!( File :: create( & rootfs_img) ) ) ) ;
129139 assert ! ( t!( child. wait( ) ) . success( ) ) ;
130140
141+ fn add_files ( w : & mut dyn Write , root : & Path , cur : & Path ) {
142+ for entry in t ! ( cur. read_dir( ) ) {
143+ let entry = t ! ( entry) ;
144+ let path = entry. path ( ) ;
145+ let to_print = path. strip_prefix ( root) . unwrap ( ) ;
146+ t ! ( write!( w, "{}\u{0} " , to_print. to_str( ) . unwrap( ) ) ) ;
147+ if t ! ( entry. file_type( ) ) . is_dir ( ) {
148+ add_files ( w, root, & path) ;
149+ }
150+ }
151+ }
152+ }
153+
154+ fn prepare_rootfs_ext4 ( rootfs : & Path , rootfs_img : & Path ) {
155+ let mut dd = Command :: new ( "dd" ) ;
156+ dd. arg ( "if=/dev/zero" )
157+ . arg ( & format ! ( "of={}" , rootfs_img. to_string_lossy( ) ) )
158+ . arg ( "bs=1M" )
159+ . arg ( "count=1024" ) ;
160+ let mut dd_child = t ! ( dd. spawn( ) ) ;
161+ assert ! ( t!( dd_child. wait( ) ) . success( ) ) ;
162+
163+ let mut mkfs = Command :: new ( "mkfs.ext4" ) ;
164+ mkfs. arg ( "-d" ) . arg ( rootfs) . arg ( rootfs_img) ;
165+ let mut mkfs_child = t ! ( mkfs. spawn( ) ) ;
166+ assert ! ( t!( mkfs_child. wait( ) ) . success( ) ) ;
167+ }
168+
169+ fn start_qemu_emulator ( target : & str , rootfs : & Path , server : & Path , tmpdir : & Path ) {
170+ let rootfs_img = & tmpdir. join ( "rootfs.img" ) ;
171+ prepare_rootfs ( target, rootfs, server, rootfs_img) ;
172+
131173 // Start up the emulator, in the background
132174 match target {
133175 "arm-unknown-linux-gnueabihf" => {
@@ -170,19 +212,30 @@ fn start_qemu_emulator(target: &str, rootfs: &Path, server: &Path, tmpdir: &Path
170212 . arg ( "virtio-net-device,netdev=net0,mac=00:00:00:00:00:00" ) ;
171213 t ! ( cmd. spawn( ) ) ;
172214 }
173- _ => panic ! ( "cannot start emulator for: {}" < target) ,
174- }
175-
176- fn add_files ( w : & mut dyn Write , root : & Path , cur : & Path ) {
177- for entry in t ! ( cur. read_dir( ) ) {
178- let entry = t ! ( entry) ;
179- let path = entry. path ( ) ;
180- let to_print = path. strip_prefix ( root) . unwrap ( ) ;
181- t ! ( write!( w, "{}\u{0} " , to_print. to_str( ) . unwrap( ) ) ) ;
182- if t ! ( entry. file_type( ) ) . is_dir ( ) {
183- add_files ( w, root, & path) ;
184- }
215+ "riscv64gc-unknown-linux-gnu" => {
216+ let mut cmd = Command :: new ( "qemu-system-riscv64" ) ;
217+ cmd. arg ( "-nographic" )
218+ . arg ( "-machine" )
219+ . arg ( "virt" )
220+ . arg ( "-m" )
221+ . arg ( "1024" )
222+ . arg ( "-bios" )
223+ . arg ( "none" )
224+ . arg ( "-kernel" )
225+ . arg ( "/tmp/bbl" )
226+ . arg ( "-append" )
227+ . arg ( "quiet console=ttyS0 root=/dev/vda rw" )
228+ . arg ( "-netdev" )
229+ . arg ( "user,id=net0,hostfwd=tcp::12345-:12345" )
230+ . arg ( "-device" )
231+ . arg ( "virtio-net-device,netdev=net0,mac=00:00:00:00:00:00" )
232+ . arg ( "-device" )
233+ . arg ( "virtio-blk-device,drive=hd0" )
234+ . arg ( "-drive" )
235+ . arg ( & format ! ( "file={},format=raw,id=hd0" , & rootfs_img. to_string_lossy( ) ) ) ;
236+ t ! ( cmd. spawn( ) ) ;
185237 }
238+ _ => panic ! ( "cannot start emulator for: {}" , target) ,
186239 }
187240}
188241
0 commit comments