From 83a3d7464ecf0be00c92f498ba5c9e6f44bc4e03 Mon Sep 17 00:00:00 2001
From: Andreas Kemnade <andreas@xxxxxxxxxxxx>
Date: Sat, 5 Dec 2009 14:06:00 +0100
Subject: [PATCH] usb-gadget: Booting from g_file_storage in cdrom mode

The g_file_storage gets upset by some non-zero bytes in the end of a
READ TOC command.

<7>00000000: 55 53 42 43 42 49 4f 53 0c 00 00 00 80 00 0a 43
<7>00000010: 00 00 00 00 00 00 00 0c 40 00 00 00 00 00 00
<7>g_file_storage gadget: SCSI command: READ TOC;  Dc=10, Di=12;  Hc=10, Hi=12
<7>g_file_storage gadget: bulk-in, length 12:
<7>00000000: 00 00 00 00 00 00 00 00 00 00 00 00
<7>g_file_storage gadget: sending command-failure status
<7>g_file_storage gadget:   sense data: SK x05, ASC x24, ASCQ x00;  info x0
<7>g_file_storage gadget: bulk-in, length 13:
<7>00000000: 55 53 42 53 42 49 4f 53 0c 00 00 00 01

And then a sector size of 2048 (instead of 512) as usual for cdrom
drives is assumed. The following should read the el torito info block,
but the BIOS gets the wrong data due to the wrong sector size (offsets
are wrong).

<7>g_file_storage gadget: bulk-out, length 31:
<7>00000000: 55 53 42 43 42 49 4f 53 00 08 00 00 80 00 0c 28
<7>00000010: 00 00 00 00 11 00 00 01 00 00 00 00 00 00 00
<7>SCSI CDB: 28 00 00 00 00 11 00 00 01 00 00 00
<7>g_file_storage gadget: SCSI command: READ(10);  Dc=10, Di=512;  Hc=12, Hi=2048
<7>g_file_storage gadget: READ(10) is buggy! Expected length 10 but we got 12
<7>g_file_storage gadget-lun0: file read 512 @ 8704 -> 512
<7>g_file_storage gadget: bulk-in, length 13:
<7>00000000: 55 53 42 53 42 49 4f 53 00 06 00 00 00

The following patch solves the problem for the BIOS. I am able to load
a kernel and an initrd with it correctly. Of course, it breaks everything
else but it can be considered as a proof of concept. I can boot from my
my g_file_storage cdrom drive with it on that thinkpad X31, on a mainboard with award
bios (with an AMD Geode 1700+ on it) and also on an INTEL D945GCLF board.