modules/adb: Fix adb with gadgetfs in stage-2
@@ -29,10 +29,41 @@ with lib;
}];
};
+ # TODO: `teardown` on the stage-1 adb task, and execute it before switch_root
boot.postBootCommands = ''
- # Restart adbd early during stage-2
+ # Kill adbd early during stage-2
${pkgs.procps}/bin/pkill -x adbd
+ '';
+
+ # This service assumes there was a single gadget configured during stage-1
+ # for ffs and adb use, and that this gadget is to be re-used.
+ # TODO: self-contained configuration of the gadget with gadget-tool here.
+ systemd.services.adbd = {
+ description = "ADB Daemon for stage-2";
+ wantedBy = [ "multi-user.target" ];
+ enable = true;
+ script = ''
${pkgs.adbd}/bin/adbd &
+
+ # Wait a bit to ensure the ffs (functionfs) component is started.
+ sleep 1
+
+ # NOTE: the ffs (functionfs) userspace component needs to already
+ # be running when we "enable" the gadget.
+ # Else we will get errno -19, ENODEV.
+ if [ -e /sys/kernel/config/usb_gadget ]; then
+ cd /sys/kernel/config/usb_gadget
+ for gadget in * ; do
+ if test -n $gadget/UDC ; then
+ # honestly this does nothing more than "echo",
+ # am only using gt to show that it exists
+ ${pkgs.gadget-tool}/bin/gt enable $gadget
+ fi
+ done
+ fi
+
+ wait
'';
};
+ };
}
