Skip to content

6. Modify and compile module

Frank1119 edited this page May 22, 2020 · 4 revisions

* This patch seems to be not enough :-( *

As stated in the README.md, the patch proposed here seems to be not enough. I have successfully patched the kernel sources, with much more changes, but it is not really possible to write a wiki page for that, because there are too many dependencies with the version of the kernel sources. For the daring ones: I used this patch: http://linuxehacking.ovh/2013/07/12/how-to-emulatore-dvd-rom-hardware-usb/

Modifying storage_common.h

This guide assumes you have executed rpi-source as root, thus installing the kernel source and headers at /root/linux

Logon to the Raspberry Pi Zero

sudo -i
cd /root/linux/drivers/usb/gadget/function

Check the number of changes

sed "/(num_sectors >= 256\*60\*75)\|num_sectors = 256\*60\*75 - 1/s/256\*60\*75/256\*60\*75\*4/" /root/linux/drivers/usb/gadget/function/storage_common.c | grep "256\*60\*75\*4"

There should be two lines where 256*60*75 is changed to 256*60*75*4.

Apply the changes

Repeat the same command but with -i (in place) added:

sed -i "/(num_sectors >= 256\*60\*75)\|num_sectors = 256\*60\*75 - 1/s/256\*60\*75/256\*60\*75\*4/" /root/linux/drivers/usb/gadget/function/storage_common.c | grep "256\*60\*75\*4"

Compile the module

mkdir /root/linux/drivers/usb/gadget/function/patched
cp /root/linux/drivers/usb/gadget/function/f_mass_storage.* /root/linux/drivers/usb/gadget/function/patched/
cp /root/linux/drivers/usb/gadget/function/storage_common.* /root/linux/drivers/usb/gadget/function/patched/
grep 'ccflags\|usb_f_mass_storage' /root/linux/drivers/usb/gadget/function/Makefile > /root/linux/drivers/usb/gadget/function/patched/Makefile
cd /root/linux/drivers/usb/gadget/function/patched
make -C /lib/modules/$(uname -r)/build M=$(pwd) modules

Check whether the module loads

modprobe libcomposite
insmod /root/linux/drivers/usb/gadget/function/patched/usb_f_mass_storage.ko
dmesg | grep taint
rmmod usb_f_mass_storage
cp /root/linux/drivers/usb/gadget/function/patched/usb_f_mass_storage.ko /usr/local/bin

The message usb_f_mass_storage: loading out-of-tree module taints kernel means first that a module that is not original has been loaded, so the kernel is compromised. Second it means that the module did load indeed. Well, that could also have been checked with lsmod, but this way you also see that it is your module that has been loaded.

Loading libcomposite explicitly is necessary, because usb_f_mass_storage is not in the right place to do that itself.