subatomicglue
Posts: 54
Joined: Sun Oct 13, 2019 5:58 pm

Re: Help me Fast Boot my pi-Zero MIDI "USB Gadget"

Wed Jan 22, 2020 7:57 pm

Somewhat related, found this PDF about optimizing boot times... Nice summary. Light on implementation details. But interesting to read through

https://www.nxp.com/files-static/traini ... UXBOOT.pdf

tqhien
Posts: 73
Joined: Thu Feb 02, 2012 10:07 am

Re: Help me Fast Boot my pi-Zero MIDI "USB Gadget"

Wed Jan 22, 2020 11:43 pm

@subatomicglue : you're right, it is an image to burn on a SDCard. My main guide for buildroot is in French, from Christophe Blaess : https://www.blaess.fr/christophe/articl ... buildroot/

@DarkElvenAngel : this was just an example to show how fast you can get a working Midi usb gadget : so around 4 seconds, which was the primary goal. Network access for developpement can be loaded after that : I added g_ether tonight, that adds roughtly 1 second to get the command prompt. And to get access to the Rpi0 and upload files, I still need ssh, which is dropbear purpose (it is also a lighweight ssh server particuliarly used in embedded environment or I didn't understand your comment about SFTP)

Code: Select all

[    2.588304] using random self ethernet address
[    2.596308] using random host ethernet address
[    2.604803] usb0: HOST MAC 4e:4b:36:12:a8:bf
[    2.612469] usb0: MAC 36:44:42:3d:9f:52
[    2.619688] using random self ethernet address
[    2.627301] using random host ethernet address
[    2.634824] g_ether gadget: Ethernet Gadget, version: Memorial Day 2008
[    2.644499] g_ether gadget: g_ether ready
[    2.651378] dwc2 20980000.usb: bound driver g_ether
[    2.778863] dwc2 20980000.usb: new device is high-speed
[    3.155044] dwc2 20980000.usb: new device is high-speed
[    3.228210] dwc2 20980000.usb: new address 10
[    3.253992] g_ether gadget: high-speed config #1: CDC Ethernet (ECM)
[    8.057781] random: dropbear: uninitialized urandom read (32 bytes read)
[    8.230011] NET: Registered protocol family 10
[    8.239432] Segment Routing with IPv6
The new image is here :https://drive.google.com/open?id=16-I_H ... EFRTjFeJFZ

USB ip address is defined as 192.168.200.2.

DarkElvenAngel
Posts: 383
Joined: Tue Mar 20, 2018 9:53 pm

Re: Help me Fast Boot my pi-Zero MIDI "USB Gadget"

Thu Jan 23, 2020 12:05 am

tqhien wrote:
Wed Jan 22, 2020 11:43 pm
@DarkElvenAngel : this was just an example to show how fast you can get a working Midi usb gadget : so around 4 seconds, which was the primary goal. Network access for developpement can be loaded after that : I added g_ether tonight, that adds roughtly 1 second to get the command prompt. And to get access to the Rpi0 and upload files, I still need ssh, which is dropbear purpose (it is also a lighweight ssh server particuliarly used in embedded environment or I didn't understand your comment about SFTP)
Firstly that's awesome taking time to build an image. My comment about SFTP which is part of openssh gives you the ability to copy files and mount the file system. I originally tried dropbear but couldn't copy files or browse files from a remote file browser. Perhaps these features aren't as important as I find them.

tqhien
Posts: 73
Joined: Thu Feb 02, 2012 10:07 am

Re: Help me Fast Boot my pi-Zero MIDI "USB Gadget"

Thu Jan 23, 2020 12:18 am

Oh, ok, I understand now. I mainly use ssh to access my Rpis and I issue "scp" command from a terminal to copy files from and to my rpis. I don't use any browser.

The image files are juste some basic usable examples (with some kernel optimizations). You can add other services, or use another ssh server, add lighttpd for serving webpages (lighter the nginx or apache) or use python-flask, etc. It is also quite a minimal and fixed system. For example, there's no apt-get command nor pip install (for python). To add packages, I need to rebuild with buildroot. But that's the goal.

DarkElvenAngel
Posts: 383
Joined: Tue Mar 20, 2018 9:53 pm

Re: Help me Fast Boot my pi-Zero MIDI "USB Gadget"

Thu Jan 23, 2020 1:00 am

tqhien wrote:
Thu Jan 23, 2020 12:18 am
Oh, ok, I understand now. I mainly use ssh to access my Rpis and I issue "scp" command from a terminal to copy files from and to my rpis. I don't use any browser.

The image files are juste some basic usable examples (with some kernel optimizations). You can add other services, or use another ssh server, add lighttpd for serving webpages (lighter the nginx or apache) or use python-flask, etc. It is also quite a minimal and fixed system. For example, there's no apt-get command nor pip install (for python). To add packages, I need to rebuild with buildroot. But that's the goal.
Did you optimize the boot process with parallelism? Or have you ever tried? You can get to the login prompt without having to wait for all modules to load. Also if you use a read-only file system you can us a tmpfs and anything that needs to create/modify a file can then work without having to unlock the file system.

Now we were working on an initramfs could you not use that with your optimize kernel to start the midi gadget and then pass on to a full raspbian image. With all kernel modules copied to the raspbian? That way the OP get the power and speed of Buildroot with the flexibility of Raspbian.

tqhien
Posts: 73
Joined: Thu Feb 02, 2012 10:07 am

Re: Help me Fast Boot my pi-Zero MIDI "USB Gadget"

Thu Jan 23, 2020 8:51 am

I didn't dig any further, as I don't think a full Raspbian is needed in most cased : more speed means less functionality, and for embedded devices, functions are restricted by design. For example, I remove some filesystem initially compiled inside the kernel, to keep only ext4 and fat. Do OP need other FS for his project ?

Buildroot allows to pick only needed packages.

subatomicglue
Posts: 54
Joined: Sun Oct 13, 2019 5:58 pm

Re: Help me Fast Boot my pi-Zero MIDI "USB Gadget"

Thu Jan 23, 2020 5:34 pm

I like the idea of simply moving MIDI (and my daemon) up in the init list (or getting an optimization with initrd to accomplish that).
If "time to midi" could happen in <10 seconds with this method, I wouldn't mind if the rest of default raspbian kept initializing after that...

That said. you're probably right that I dont need a lot in my system and can certainly strip it down with buildroot! I wont miss what I wasn't using for sure. Though it might make development more of a pain with parts missing anytime I may like to add something, but evaluating my project, I think it's minimal requirements and should be no feature creep surprises coming up, at least in packages/services to add...


I have automated scripts I run from macosx that "prepare a microsd card", and "install into a running pi using SSH" (it's a 2 script process). Basically these: fetch raspbian if not downloaded already, use dd to burn a microsd card, auto change some params in config.txt and cmdline.txt for OTG SSH access and my HDMI display, auto install ssh keys for faster development, auto copy/register/start my service, auto install some utilities I use for readonly mode (busybox-syslogd ntp), and enable readonly mode, etc. These scripts make it really easy to experiment with new cards, and lock in configurations.. Reproducability rocks. And having a history of my system configs in source control is an added beneficial dimension as well. I'll dig in, but hoping that buildroot can be automated by my scripts also.

I have an additional box, a Pi3, that I will optimize later, which has libSDL2 on it for PCM audio. So 2 projects total. One's a synth, and one's a midi controller for it. For now, I'll focus on the midi controller, and I'm sure the lessons learned there will carry over to the synth.

thanks for the discussion/explanations. I'll have time this weekend. work's been busy this week.
I did take a look at the Christophe Blaess tutorial, and it looks just like what I was hoping for - thank you for this!!

subatomicglue
Posts: 54
Joined: Sun Oct 13, 2019 5:58 pm

Re: Help me Fast Boot my pi-Zero MIDI "USB Gadget"

Sun Jan 26, 2020 7:48 pm

Zipped file image is 47Mo and only 512Mo minimum SD Card is required : boot partition is 34Mo, rootfs is 255Mo (with plenty of space in it...) and it is available here : https://drive.google.com/open?id=1LFPRR ... FM0xSXSiio

root password is rpi and there's an autologin for user rpi / password rpi

Have fun !

...

The new image is here :https://drive.google.com/open?id=16-I_H ... EFRTjFeJFZ
USB ip address is defined as 192.168.200.2.
I got a chance to try these two images this weekend.
I used this command to burn the MicroSD card - running under MacOSX, which I use all the time for burning the official Buster image:

Code: Select all

# 1st image from tqhien
$ stat -f%z ~/Downloads/usb-midi-exemple.img
1977614336
$ echo `expr 1977614336 / 1048576`    # divide by number of bytes in a MB  (1024x1024)
1886
$ dd bs=1m if=/Users/username/Downloads/usb-midi-exemple.img count=1887 | pv -s 1977614336 | dd of=/dev/disk2 bs=1m

# 2nd image from tqhien
$ stat -f%z ~/Downloads/usb-midi-n-ether.img
1977614336
$ echo `expr 1977614336 / 1048576`    # divide by number of bytes in a MB  (1024x1024)
1886
$ dd bs=1m if=/Users/username/Downloads/usb-midi-n-ether.img count=1887 | pv -s 1977614336 | dd of=/dev/disk2 bs=1m
This is how I burn buster, which always works (and I reverified it still works, just now):

Code: Select all

$ stat -f%z 2019-09-26-raspbian-buster-lite.img
2248146944
$ echo `expr 2248146944 / 1048576`    # divide by number of bytes in a MB  (1024x1024)
2144
$ dd bs=1m if=./2019-09-26-raspbian-buster-lite.img count=2145 | pv -s 2248146944 | dd of=/dev/disk2 bs=1m
I couldn't get either to work. It might just be me...

Setup:
- pi zero
- ada fruit HDMI display connected
- USB cable from MacBookPro, connected to left hand USB connector on the pizero.

1.) with my adafruit 800x400 hdmi display attached, all I see is a rainbow screen. text screen never happens, so I can't tell what's going on.
I did have to add this to config.txt

Code: Select all

# setup for adafruit HDMI 800x480 touchscreen
hdmi_force_hotplug=1

# force a specific HDMI mode (here we are forcing 800x480!)
hdmi_group=2
hdmi_mode=87
hdmi_cvt=800 480 60 6 0 0 0
hdmi_drive=1
2.) I cant SSH in...

Code: Select all

$ arp -a

? (10.37.129.2) at 0:1c:42:0:0:9 on vnic1 ifscope permanent [ethernet]
? (10.211.55.2) at 0:1c:42:0:0:8 on vnic0 ifscope permanent [ethernet]
? (192.168.1.1) at 24:a2:e1:e7:eb:19 on en0 ifscope [ethernet]
? (192.168.1.72) at 8:5:81:23:f:8d on en0 ifscope [ethernet]
? (192.168.1.157) at fc:2a:9c:bd:37:62 on en0 ifscope [ethernet]
? (192.168.1.162) at c4:b3:1:c4:c3:c3 on en0 ifscope permanent [ethernet]
? (192.168.1.171) at f8:2d:7c:17:c2:c3 on en0 ifscope [ethernet]
? (192.168.1.180) at ac:bc:32:b2:47:49 on en0 ifscope [ethernet]
? (224.0.0.251) at 1:0:5e:0:0:fb on en0 ifscope permanent [ethernet]
? (239.255.255.250) at 1:0:5e:7f:ff:fa on en0 ifscope permanent [ethernet]
192.168.1.180 showed up after plugging my pizero into the macbookpro... but no response with ssh 192.168.1.162, 10.211.55.2, and 10.37.129.2 all respond with a password prompt, but, rpi doesn't work to get in... (neither does pi and raspberry)

no luck with ssh [email protected] either
no luck with the IP address you gave above: ssh [email protected] either. no response (no password prompt)

I was able to create my own image as well, using buildroot. But same rainbowscreen, and no ssh ability...

3.) No MIDI device appeared in the "Audio MIDI Setup" app under utilities....

4.) USB dongle plugged into the left hand pizero USB port... Activity lights, light up for a stock buster image - but not for either of the 2 images from tqhien. so i'm not seeing obvious ethernet from either image

ideas?
Last edited by subatomicglue on Mon Jan 27, 2020 3:40 am, edited 4 times in total.

DarkElvenAngel
Posts: 383
Joined: Tue Mar 20, 2018 9:53 pm

Re: Help me Fast Boot my pi-Zero MIDI "USB Gadget"

Mon Jan 27, 2020 1:02 am

Buildroot isn't like raspbian and won't do much for you you have to set a lot up by hand. By the sounds of it though your build isn't working at all. What steps did you follow?

subatomicglue
Posts: 54
Joined: Sun Oct 13, 2019 5:58 pm

Re: Help me Fast Boot my pi-Zero MIDI "USB Gadget"

Mon Jan 27, 2020 1:21 am

Not just my build from buildroot, but also the 2 builds that tqhien built...
I'd like to get those 2 working first - since they should just work... (tested by tqhien)...

(for my build, I followed: https://www.blaess.fr/christophe/articl ... buildroot/)

subatomicglue
Posts: 54
Joined: Sun Oct 13, 2019 5:58 pm

Re: Help me Fast Boot my pi-Zero MIDI "USB Gadget"

Mon Jan 27, 2020 1:29 am

Ok, since neither the built images from tqhien nor my own buildroot was working for me, I worked more on the initrd approach from before.

So I added a script, /etc/initramfs-tools/scripts/init-premount/runme, intended to initialize the USB gadget and midi stuff...

and then I noticed that dmesg doesn't give you all the text! And dmesg is what I was using to debug(!!!)...
And all I saw was something like:

Code: Select all

[    0.252494] Trying to unpack rootfs image as initramfs...
[    1.391310] Freeing initrd memory: 8780K
not helpful.

But if I watch the pizero boot up on an HDMI display, I see MORE THINGS!
Look at [ 5.39] in the boot sequence, 13seconds in:
https://youtu.be/qpUGQggEuzc?t=13

Ignore the /sys and /dev errors in the video, that was some debugging I had left in...
Relevant Errors are:

Code: Select all

Begin: Loading essential drivers ... done.
Begin: Running /scripts/init-premount ... creating composite mass-storage, serial, ethernet, hid, midi....
INIT USB GADGET
/scripts/init-premount/runme: cd: line 49: can't cd to /sys/kernel/config/usb_gadget/: No such file or directory
/scripts/init-premount/runme: cd: line 66: can't cd to /sys/kernel/config/usb_gadget/midi_over_usb: No such file or directory
SETUP DEVICES
- MIDI DEVICE USB
END SETUP
done
I read somewhere you could init /dev with

Code: Select all

echo /sbin/mdev > /proc/sys/kernel/hotplug
mdev -s
mount -n -t proc none /proc
mount -n -t sysfs none /sys
mount -n -t devtmpfs none /dev

# do the USB and midi init stuff

umount /proc
umount /sys
umount /dev
But this results in the following errors appearing before the above errors

Code: Select all

mount: mounting none on /proc failed: Device or resource busy
mount: mounting none on /sys failed: Device or resource busy
mount: mounting none on /dev failed: Device or resource busy
Then the errors above (e.g. can't cd to /sys/kernel/config/usb_gadget/: No such file or directory)
Then an endless loop on cat: can't open /proc/uptime. Probably caused by my mounts :)

DarkElvenAngel
Posts: 383
Joined: Tue Mar 20, 2018 9:53 pm

Re: Help me Fast Boot my pi-Zero MIDI "USB Gadget"

Mon Jan 27, 2020 2:11 am

As far as Buildroot images go try using etcher.

As for the initrd when you're script is running the /sys is not mounted when your script is running. You can unpack it and look how it works.

Code: Select all

mkdir /tmp/initramfs
 cd /tmp/initramfs
 zcat /boot/initrd.img-4.19.75-v7l+ | cpio -idmv
Remember to use the correct file name

This might help you understand better the error you see and how it might be fixed

subatomicglue
Posts: 54
Joined: Sun Oct 13, 2019 5:58 pm

Re: Help me Fast Boot my pi-Zero MIDI "USB Gadget"

Mon Jan 27, 2020 2:28 am

This is the "runme" script I'm running from initramfs

Code: Select all

$ cat /etc/initramfs-tools/scripts/init-premount/runme

#!/bin/sh
# Example usb gadget boot script

PREREQ=""
prereqs()
{
  echo "$PREREQ"
}

case $1 in
prereqs)
  prereqs
  exit 0
  ;;
esac

echo /sbin/mdev > /proc/sys/kernel/hotplug

# Mount the /proc and /sys filesystems.
mount -t proc none /proc
mount -t sysfs none /sys
mount -t devtmpfs none /dev

busybox --install -s
mknod /dev/null c 1 3
mknod /dev/tty c 5 0
mdev -s

##########################################
# INIT LIBCOMPOSITE
# no need to add g_midi, g_ether, etc, to /etc/modules, we'll do it all here with libcomposite
echo "creating composite mass-storage, serial, ethernet, hid, midi..."
modprobe dwc2 # if not already loaded in /etc/modules
modprobe libcomposite

##########################################
# INIT USB GADGET
echo "INIT USB GADGET"
cd /sys/kernel/config/usb_gadget/
rm -rf midi_over_usb # clear out old if there...
mkdir -p midi_over_usb
cd midi_over_usb

echo 0x1d6b > idVendor # Linux Foundation
echo 0x0104 > idProduct # Multifunction Composite Gadget
echo 0x0100 > bcdDevice # v1.0.0
echo 0x0200 > bcdUSB # USB2

mkdir -p strings/0x409
echo "asdfghlMantis307SubatomicLabs34579381" > strings/0x409/serialnumber
echo "manufacturer" > strings/0x409/manufacturer
echo "MIDI Controller" > strings/0x409/product


# assume this exists - must call otg-setup first!
cd /sys/kernel/config/usb_gadget/midi_over_usb
N="usb0"
C=1

##########################################
# SETUP DEVICES
echo "SETUP DEVICES"

# MIDI DEVICE USB
# See new device:  amidi -l
# Get device port: aplaymidi --list
# Sample File:     curl https://www.midiworld.com/download/3861 > techno.midi
# Play MIDI Out:   aplaymidi --port 20:0 techno.midi
echo " - MIDI DEVICE USB"
mkdir -p functions/midi.$N
# id     - ID string for the USB MIDI adapter
echo "pi-zero-midi" > functions/midi.$N/id
mkdir -p configs/c.$C
ln -s functions/midi.$N configs/c.$C/

#########################################
# END SETUP (this refreshes the system)
echo "END SETUP"

# this lists available UDC drivers
# echo "" > UDC && ls /sys/class/udc > UDC
ls /sys/class/udc > UDC
#########################################

umount /proc
umount /sys
umount /dev

echo "done"
exit 0

subatomicglue
Posts: 54
Joined: Sun Oct 13, 2019 5:58 pm

Re: Help me Fast Boot my pi-Zero MIDI "USB Gadget"

Mon Jan 27, 2020 2:29 am

I have been using the lsinitramfs command, so I know what is inside the image...

I also know the /dev and /sys directories are virtual and aren't included... Which is why I'm trying to mount them....

DarkElvenAngel
Posts: 383
Joined: Tue Mar 20, 2018 9:53 pm

Re: Help me Fast Boot my pi-Zero MIDI "USB Gadget"

Mon Jan 27, 2020 2:44 am

subatomicglue wrote:
Mon Jan 27, 2020 2:29 am
I have been using the lsinitramfs command, so I know what is inside the image...

I also know the /dev and /sys directories are virtual and aren't included... Which is why I'm trying to mount them....
I just learned something I never knew there was a lsinitramfs command! Here is the init script running inside the initramfs

Code: Select all

#!/bin/sh

# Default PATH differs between shells, and is not automatically exported
# by klibc dash.  Make it consistent.
export PATH=/sbin:/usr/sbin:/bin:/usr/bin

[ -d /dev ] || mkdir -m 0755 /dev
[ -d /root ] || mkdir -m 0700 /root
[ -d /sys ] || mkdir /sys
[ -d /proc ] || mkdir /proc
[ -d /tmp ] || mkdir /tmp
mkdir -p /var/lock
mount -t sysfs -o nodev,noexec,nosuid sysfs /sys
mount -t proc -o nodev,noexec,nosuid proc /proc

case " $(cat /proc/cmdline) " in
*\ quiet\ *)
	quiet=y
	;;
*)
	quiet=n
	echo "Loading, please wait..."
	;;
esac
export quiet

# Note that this only becomes /dev on the real filesystem if udev's scripts
# are used; which they will be, but it's worth pointing out
mount -t devtmpfs -o nosuid,mode=0755 udev /dev
mkdir /dev/pts
mount -t devpts -o noexec,nosuid,gid=5,mode=0620 devpts /dev/pts || true
mount -t tmpfs -o "noexec,nosuid,size=10%,mode=0755" tmpfs /run
mkdir -m 0755 /run/initramfs

# Export the dpkg architecture
export DPKG_ARCH=
. /conf/arch.conf

# Set modprobe env
export MODPROBE_OPTIONS="-qb"

# Export relevant variables
export ROOT=
export ROOTDELAY=
export ROOTFLAGS=
export ROOTFSTYPE=
export IP=
export BOOT=
export BOOTIF=
export UBIMTD=
export break=
export init=/sbin/init
export readonly=y
export rootmnt=/root
export debug=
export panic=
export blacklist=
export resume=
export resume_offset=
export drop_caps=
export fastboot=n
export forcefsck=n
export fsckfix=


# Bring in the main config
. /conf/initramfs.conf
for conf in conf/conf.d/*; do
	[ -f ${conf} ] && . ${conf}
done
. /scripts/functions

# Parse command line options
for x in $(cat /proc/cmdline); do
	case $x in
	init=*)
		init=${x#init=}
		;;
	root=*)
		ROOT=${x#root=}
		if [ -z "${BOOT}" ] && [ "$ROOT" = "/dev/nfs" ]; then
			BOOT=nfs
		fi
                ;;
	rootflags=*)
		ROOTFLAGS="-o ${x#rootflags=}"
		;;
	rootfstype=*)
		ROOTFSTYPE="${x#rootfstype=}"
		;;
	rootdelay=*)
		ROOTDELAY="${x#rootdelay=}"
		case ${ROOTDELAY} in
		*[![:digit:].]*)
			ROOTDELAY=
			;;
		esac
		;;
	nfsroot=*)
		NFSROOT="${x#nfsroot=}"
		;;
	ip=*)
		IP="${x#ip=}"
		;;
	boot=*)
		BOOT=${x#boot=}
		;;
	ubi.mtd=*)
		UBIMTD=${x#ubi.mtd=}
		;;
	resume=*)
		RESUME="${x#resume=}"
		case $RESUME in
	        UUID=*)
			RESUME="/dev/disk/by-uuid/${RESUME#UUID=}"
		esac
		;;
	resume_offset=*)
		resume_offset="${x#resume_offset=}"
		;;
	noresume)
		noresume=y
		;;
	drop_capabilities=*)
		drop_caps="-d ${x#drop_capabilities=}"
		;;
	panic=*)
		panic="${x#panic=}"
		case ${panic} in
		*[![:digit:].]*)
			panic=
			;;
		esac
		;;
	ro)
		readonly=y
		;;
	rw)
		readonly=n
		;;
	debug)
		debug=y
		quiet=n
		if [ -n "${netconsole}" ]; then
			exec >/dev/kmsg 2>&1
		else
			exec >/run/initramfs/initramfs.debug 2>&1
		fi
		set -x
		;;
	debug=*)
		debug=y
		quiet=n
		set -x
		;;
	break=*)
		break=${x#break=}
		;;
	break)
		break=premount
		;;
	blacklist=*)
		blacklist=${x#blacklist=}
		;;
	netconsole=*)
		netconsole=${x#netconsole=}
		[ "x$debug" = "xy" ] && exec >/dev/kmsg 2>&1
		;;
	BOOTIF=*)
		BOOTIF=${x#BOOTIF=}
		;;
	fastboot|fsck.mode=skip)
		fastboot=y
		;;
	forcefsck|fsck.mode=force)
		forcefsck=y
		;;
	fsckfix|fsck.repair=yes)
		fsckfix=y
		;;
	fsck.repair=no)
		fsckfix=n
		;;
	esac
done

# Default to BOOT=local if no boot script defined.
if [ -z "${BOOT}" ]; then
	BOOT=local
fi

if [ -n "${noresume}" ] || [ "$RESUME" = none ]; then
	export noresume=y
	unset resume
else
	resume=${RESUME:-}
fi

maybe_break top

# Don't do log messages here to avoid confusing graphical boots
run_scripts /scripts/init-top

maybe_break modules
[ "$quiet" != "y" ] && log_begin_msg "Loading essential drivers"
[ -n "${netconsole}" ] && modprobe netconsole netconsole="${netconsole}"
load_modules
[ "$quiet" != "y" ] && log_end_msg

if [ "$ROOTDELAY" ]; then
	sleep $ROOTDELAY
fi

maybe_break premount
[ "$quiet" != "y" ] && log_begin_msg "Running /scripts/init-premount"
run_scripts /scripts/init-premount
[ "$quiet" != "y" ] && log_end_msg

maybe_break mount
log_begin_msg "Mounting root file system"
# Always load local and nfs (since these might be needed for /etc or
# /usr, irrespective of the boot script used to mount the rootfs).
. /scripts/local
. /scripts/nfs
. /scripts/${BOOT}
parse_numeric ${ROOT}
maybe_break mountroot
mount_top
mount_premount
mountroot
log_end_msg

if read_fstab_entry /usr; then
	log_begin_msg "Mounting /usr file system"
	mountfs /usr
	log_end_msg
fi

# Mount cleanup
mount_bottom
nfs_bottom
local_bottom

maybe_break bottom
[ "$quiet" != "y" ] && log_begin_msg "Running /scripts/init-bottom"
# We expect udev's init-bottom script to move /dev to ${rootmnt}/dev
run_scripts /scripts/init-bottom
[ "$quiet" != "y" ] && log_end_msg

# Move /run to the root
mount -n -o move /run ${rootmnt}/run

validate_init() {
	run-init -n "${rootmnt}" "${1}"
}

# Check init is really there
if ! validate_init "$init"; then
	echo "Target filesystem doesn't have requested ${init}."
	init=
	for inittest in /sbin/init /etc/init /bin/init /bin/sh; do
		if validate_init "${inittest}"; then
			init="$inittest"
			break
		fi
	done
fi

# No init on rootmount
if ! validate_init "${init}" ; then
	panic "No init found. Try passing init= bootarg."
fi

maybe_break init

# don't leak too much of env - some init(8) don't clear it
# (keep init, rootmnt, drop_caps)
unset debug
unset MODPROBE_OPTIONS
unset DPKG_ARCH
unset ROOTFLAGS
unset ROOTFSTYPE
unset ROOTDELAY
unset ROOT
unset IP
unset BOOT
unset BOOTIF
unset UBIMTD
unset blacklist
unset break
unset noresume
unset panic
unset quiet
unset readonly
unset resume
unset resume_offset
unset fastboot
unset forcefsck
unset fsckfix

# Move virtual filesystems over to the real filesystem
mount -n -o move /sys ${rootmnt}/sys
mount -n -o move /proc ${rootmnt}/proc

# Chain to real filesystem
exec run-init ${drop_caps} ${rootmnt} ${init} "[email protected]" <${rootmnt}/dev/console >${rootmnt}/dev/console 2>&1
echo "Something went badly wrong in the initramfs."
panic "Please file a bug on initramfs-tools."
Your script may be running at the wrong point?

subatomicglue
Posts: 54
Joined: Sun Oct 13, 2019 5:58 pm

Re: Help me Fast Boot my pi-Zero MIDI "USB Gadget"

Mon Jan 27, 2020 3:12 am

Thanks for the init script source, that helps.
From that, I think init-bottom runs dead last.
Regardless init-premount seems to happen after the sys stuff...

I tried my script there, and still the same errors starting with:

mkdir -p /sys/kernel/config/usb_gadget/
mkdir: can't create directory '/sys/kernel/config/usb_gadget/': No such file or directory

I'm betting /sys exists, but usb_gadget not...

I tried this with and without the mdev lines below...

Code: Select all

$ cat /etc/initramfs-tools/scripts/init-bottom/runme

#!/bin/sh
# Example usb gadget boot script

PREREQ=""
prereqs()
{
  echo "$PREREQ"
}

case $1 in
prereqs)
  prereqs
  exit 0
  ;;
esac

# I tried with and without these lines:
#echo /sbin/mdev > /proc/sys/kernel/hotplug
#mdev -s

##########################################
# INIT LIBCOMPOSITE
# no need to add g_midi, g_ether, etc, to /etc/modules, we'll do it all here with libcomposite
echo "creating composite mass-storage, serial, ethernet, hid, midi..."
modprobe dwc2 # if not already loaded in /etc/modules
modprobe libcomposite

##########################################
# INIT USB GADGET
echo "INIT USB GADGET"
mkdir -p /sys/kernel/config/usb_gadget/
cd /sys/kernel/config/usb_gadget/
rm -rf midi_over_usb # clear out old if there...
mkdir -p midi_over_usb
cd midi_over_usb

echo 0x1d6b > idVendor # Linux Foundation
echo 0x0104 > idProduct # Multifunction Composite Gadget
echo 0x0100 > bcdDevice # v1.0.0
echo 0x0200 > bcdUSB # USB2

mkdir -p strings/0x409
echo "asdfghlMantis307SubatomicLabs34579381" > strings/0x409/serialnumber
echo "subatomiclabs" > strings/0x409/manufacturer
echo "Mantis307 Controller" > strings/0x409/product


# assume this exists - must call otg-setup first!
cd /sys/kernel/config/usb_gadget/midi_over_usb
N="usb0"
C=1

##########################################
# SETUP DEVICES
echo "SETUP DEVICES"

# MIDI DEVICE USB
# See new device:  amidi -l
# Get device port: aplaymidi --list
# Sample File:     curl https://www.midiworld.com/download/3861 > techno.midi
# Play MIDI Out:   aplaymidi --port 20:0 techno.midi
echo " - MIDI DEVICE USB"
mkdir -p functions/midi.$N
# id     - ID string for the USB MIDI adapter
echo "pi-zero-midi" > functions/midi.$N/id
mkdir -p configs/c.$C
ln -s functions/midi.$N configs/c.$C/

#########################################
# END SETUP (this refreshes the system)
echo "END SETUP"

# this lists available UDC drivers
# echo "" > UDC && ls /sys/class/udc > UDC
ls /sys/class/udc > UDC
#########################################

echo "done"
exit 0

subatomicglue
Posts: 54
Joined: Sun Oct 13, 2019 5:58 pm

Re: Help me Fast Boot my pi-Zero MIDI "USB Gadget"

Mon Jan 27, 2020 3:22 am

I added a few diagnostics to the script, and I can confirm that

/sys/kernel/config/

does exist. while

/sys/kernel/config/usb_gadget/

does not. Nor can it be created (using mkdir)

Code: Select all

mkdir -p /sys/kernel/config/usb_gadget/
mkdir: can't create directory '/sys/kernel/config/usb_gadget/': No such file or directory

DarkElvenAngel
Posts: 383
Joined: Tue Mar 20, 2018 9:53 pm

Re: Help me Fast Boot my pi-Zero MIDI "USB Gadget"

Mon Jan 27, 2020 3:31 am

subatomicglue wrote:
Mon Jan 27, 2020 3:22 am
I added a few diagnostics to the script, and I can confirm that

/sys/kernel/config/

does exist. while

/sys/kernel/config/usb_gadget/

does not. Nor can it be created (using mkdir)

Code: Select all

mkdir -p /sys/kernel/config/usb_gadget/
mkdir: can't create directory '/sys/kernel/config/usb_gadget/': No such file or directory
If you modprobe the modules in say init-top and then say check if they are loaded in a different script later in the process does this work?

subatomicglue
Posts: 54
Joined: Sun Oct 13, 2019 5:58 pm

Re: Help me Fast Boot my pi-Zero MIDI "USB Gadget"

Mon Jan 27, 2020 3:42 am

I already modprobe in this script

Code: Select all

modprobe dwc2 # if not already loaded in /etc/modules
modprobe libcomposite
Are you thinking it may need some time between the calls to modprobe, and the mkdir?

DarkElvenAngel
Posts: 383
Joined: Tue Mar 20, 2018 9:53 pm

Re: Help me Fast Boot my pi-Zero MIDI "USB Gadget"

Mon Jan 27, 2020 3:49 am

Are you thinking it may need some time between the calls to modprobe, and the mkdir?
Yes, exactly how much time is the question. You could use a while loop to wait for the modules to load but I would put a time out in so you don't lockup your boot.

I'm thinking that you might need to customize things more than initramfs-tools is allowing.

This guide was helpful to me when I needed to learn how to customize my initramfs to suit my needs. You just have to remember it's geared towards Gentoo not Raspbian. And the initramfs generated by initramfs-tools can be used as a starting point so you can see how it works.
https://wiki.gentoo.org/wiki/Custom_Initramfs

The only thing you will want to do is install busybox-static if you're going to go full custom it makes things easier not having to find libraries.

subatomicglue
Posts: 54
Joined: Sun Oct 13, 2019 5:58 pm

Re: Help me Fast Boot my pi-Zero MIDI "USB Gadget"

Mon Jan 27, 2020 3:53 am

I added in

Code: Select all

lsmod | grep dwc2
lsmod | grep libcomposite
modprobe dwc2
modprobe libcomposite
lsmod | grep dwc2
lsmod | grep libcomposite
In both the init-top and in the init-bottom

for init top, I see only dwc2 and libcomposite loaded
for init-bottom, I see dwc2, libcomposite, usb_f_midi, usb_f_ecm, u_ether, udc_core seem to all show up

Still get the "mkdir: can't create directory '/sys/kernel/config/usb_gadget': No such file or Directory"

subatomicglue
Posts: 54
Joined: Sun Oct 13, 2019 5:58 pm

Re: Help me Fast Boot my pi-Zero MIDI "USB Gadget"

Mon Jan 27, 2020 3:56 am

I added a sleep 5 in the script before it creates the /sys/kernel/config/usb_gadget/ directory...
I'd have expected 5 seconds to be plenty to have those load... but same error as before

mkdir -p /sys/kernel/config/usb_gadget/
mkdir: can't create directory '/sys/kernel/config/usb_gadget/': No such file or directory

DarkElvenAngel
Posts: 383
Joined: Tue Mar 20, 2018 9:53 pm

Re: Help me Fast Boot my pi-Zero MIDI "USB Gadget"

Mon Jan 27, 2020 4:02 am

subatomicglue wrote:
Mon Jan 27, 2020 3:56 am
I added a sleep 5 in the script before it creates the /sys/kernel/config/usb_gadget/ directory...
I'd have expected 5 seconds to be plenty to have those load... but same error as before
What if you force the initramfs to the rescue terminal then you can see if all this works from the command line?

Then if this doesn't work you know and if it does you could merge the process into a script that should work.

subatomicglue
Posts: 54
Joined: Sun Oct 13, 2019 5:58 pm

Re: Help me Fast Boot my pi-Zero MIDI "USB Gadget"

Mon Jan 27, 2020 4:45 am

Thanks, reading about the rescue shell now
https://wiki.debian.org/InitramfsDebug

subatomicglue
Posts: 54
Joined: Sun Oct 13, 2019 5:58 pm

Re: Help me Fast Boot my pi-Zero MIDI "USB Gadget"

Mon Jan 27, 2020 5:40 am

Allright

I added break=init to the /boot/cmdline.txt, and using my HDMI display, I can see it drop into the (initramfs) command prompt, right after my script ran.

If I type lsmod nothing is listed...

Code: Select all

(initramfs) lsmod
Module                         Size           Used by                Not tainted
(initramfs)
If I type modprobe dwc2; modprobe libcomposite, which are both totally silent, even with -v -f switches
then lsmod, still nothing listed
cat /proc/modules is also empty

Same result when using break=premount ... and break=bottom ...

And these do exist:
$ lsinitramfs /boot/initrd.img-4.19.75+ | grep dwc
usr/lib/modules/4.19.75+/kernel/drivers/usb/dwc2
usr/lib/modules/4.19.75+/kernel/drivers/usb/dwc2/dwc2.ko

$ lsinitramfs /boot/initrd.img-4.19.75+ | grep libcomposite
usr/lib/modules/4.19.75+/kernel/drivers/usb/gadget/libcomposite.ko

Return to “Advanced users”