[Nook Touch General] Aqq123:
More details about the boot sequence

Just as an update to the update, the name is actually supposed to be MLO (all uppercase). Although the NST's boot partition (mmcblk0p1) is a case-insensitive filesystem (FAT), so it doesn't matter in this case (no pun intended) but maybe in some different usage scenario it'll save someone some searching, so I'm posting this here. The next-stage bootloader, u-boot.bin, is all lowercase (hardcoded in MLO), as are cfg.bin, flash_spl.bin, wvf.bin, and the boot-time logo booting.pgm (hardcoded in u-boot.bin). The names uImage and uRamdisk can be modified through boot.scr (also hardcoded in u-boot.bin), which is generated from boot.script with:

mkimage -A arm -O linux -T script -C none -a 0 -e 0 -d boot.script boot.scr

The file boot.script serves no other purpose and can be removed once boot.scr is generated.

The complete boot sequence

Here's the whole boot process in detail. I rearranged the order to make it more readable, and removed the uninteresting stuff.

Variables: initial state

[B]commonbootargs=[/B]console=ttyS0,115200n8 initrd rw init=/init vram=16M video=omap3epfb:mode=800x600x16x14x270x0,pmic=${epd_pmic},vcom=${vcom} androidboot.console=ttyS0
[B]setbootargs=[/B]setenv indirectba setenv bootargs [B]$commonbootargs[/B];run indirectba
[B]bootvar=normalboot[/B]

The main logic

[B]bootcmd[/B]=run [B]autodetectmmc[/B];if run [B]loadbootscript[/B]; then run [B]bootscript[/B];else run [B]readtokens[/B]; run [B]checkbootcount[/B]; run [B]checkrom[/B]; run [B]checkupdate[/B]; run [B]checkbcb[/B]; run [B]${bootvar}[/B];fi

The tests

Boot from SD card if present

[B]autodetectmmc=[/B]if itest.s ${bootdevice} == "SD"; then setenv mmcbootdev 0; setenv mmcromdev 0;else setenv mmcbootdev 1; setenv mmcromdev 1; setenv mmcrootdev 1;fi;mmcinit $mmcbootdev

Load

boot.scr

if present

[B]loadbootscript=[/B]fatload mmc ${mmcbootdev} ${loadaddr} [U]boot.scr[/U]
[B]bootscript=[/B]echo [I]Running bootscript from ${bootdevice}...[/I]; autoscr ${loadaddr}

Otherwise if not using boot script then

[B]readtokens=[/B]if fatload mmc ${mmcromdev}:2 0x81c00000 [U]devconf/DeviceID[/U] 100; then setenvmem serialnum 0x81c00000 0x$filesize;fi;if fatload mmc ${mmcromdev}:2 0x81c00000 [U]devconf/EpdVcom[/U] 20; then setenvmem vcom 0x81c00000 0x$filesize;fi

Check boot count

Load boot count from mmcblk0p2:

/devconf/BootCnt

. If negative, reset to 0. If higher than 7, load

recovery --restore=factory

with mmcblk0p2:

/factory.zip

. Otherwise, proceed normally.

[B]checkbootcount=[/B] if fatload mmc ${mmcromdev}:2 0x81c00000 [U]devconf/BootCnt[/U] 4; then calc.l *0x81c00000 + 1 0x81c00000; if itest.l *0x81c00000 < 0; then echo [I]Bad Boot count (Fixed)[/I]; mw.l 0x81c00000 0 1; fatsave mmc ${mmcromdev}:2 0x81c00000 [U]devconf/BootCnt[/U] 4; fi; if itest.l *0x81c00000 > 7; then echo [I]Boot count exceeded[/I]; echo [I]Forced to boot into recovery mode for Factory Fallback[/I]; mw.b 0x81c00000 0 0x200; cp.s "boot-recovery" 0x81c00000 0xd; cp.s "[U]recovery --restore=factory --update_package=MISC:factory.zip[/U]" 0x81c00040 0x3d; mw.b 0x81c00048 0xa 1; mw.b 0x81c0005a 0xa 1; fatsave mmc ${mmcromdev}:2 0x81c00000 BCB 0x200; mw.l 0x81c00000 0 1; fatsave mmc ${mmcromdev}:2 0x81c00000 [U]devconf/BootCnt[/U] 4; else echo [I]Boot count OK[/I]; fi;fi

Check if "ROM" is "bad"

Assume "bad" if cannot load mmcblk0p2:

/devconf/DeviceID

. Then, load

recovery --restore=rom

with mmcblk0p1:

/romrestore.zip

. Otherwise, proceed normally.

[B]checkrom=[/B]mmcinit ${mmcromdev};fatload mmc ${mmcromdev}:2 0x81c00000 [U]devconf/DeviceID[/U] 1;setenv badrom $?;fatload mmc ${mmcromdev}:2 0x81c00000 devconf/BootCnt 4;if itest.l $? -ne 0 || itest.l 0x$filesize -ne 0x4 || itest.l $badrom -ne 0; then echo [I]Forced to boot into recovery mode for ROM restore[/I]; mw.b 0x81c00000 0 0x200; cp.s "boot-recovery" 0x81c00000 0xd; cp.s "[U]recovery --restore=rom --update_package=BOOT:romrestore.zip[/U]" 0x81c00040 0x3c; mw.b 0x81c00048 0xa 1; mw.b 0x81c00056 0xa 1; fatsave mmc ${mmcromdev}:2 0x81c00000 BCB 0x200;fi

Check if there is an update to be installed

If there's a file mmcblk1p1:

/gossamer_update.zip

(that is, in the root directory of the first partition on the SD card), run

recovery

with the path to this package.

[B]checkupdate[/B]=mmcinit 0;if itest.s ${resettype} -eq "cold"; then if fatload mmc 0:1 0x81c00000 [U]gossamer_update.zip[/U] 1; then echo [I]Forced to boot into recovery mode for SD update[/I]; mw.b 0x81c00000 0 0x200; cp.s "boot-recovery" 0x81c00000 0xd; cp.s "[U]recovery --update_package=SDCARD:gossamer_update.zip[/U]" 0x81c00040 0x35; mw.b 0x81c00048 0xa 1; mmcinit ${mmcromdev}; fatsave mmc ${mmcromdev}:2 0x81c00000 BCB 0x200; else mmcinit ${mmcromdev}; fi;fi

Check if battery status abnormal

If force_recovery equal to 1 or 2, proceed into recovery (bcbcharging or bcbreset respectively). This parameter appears to be set by some lower-level routine depending on the battery status. Also proceed into recovery If mmcblk0p2:

/BCB

fails some sanity checks.

[B]checkbcb=[/B]mmcinit $mmcbootdev;if itest ${forcerecovery} -eq 1; then echo [I]Forced to boot into recovery mode for charging[/I]; run [B]bcbcharging[/B]; setenv [B]bootvar recoveryboot[/B];fi; if itest ${forcerecovery} -eq 2; then echo [I]Forced to boot into recovery mode for reset[/I]; run [B]bcbreset[/B]; setenv [B]bootvar recoveryboot[/B];fi; fatload mmc ${mmcromdev}:2 0x81c00000 [U]BCB[/U] 0x1000;if itest.l $? -ne 0; then echo [I]Missing BCB forces recovery mode[/I]; setenv [B]bootvar recoveryboot[/B];elif itest.l 0x$filesize -lt 0x200; then echo [I]Empty BCB forces recovery mode[/I]; setenv [B]bootvar recoveryboot[/B];elif itest.b *0x81c00040 -ne 0 && itest.s *0x81c00040 == "recovery"; then echo [I]BCB forces recovery mode[/I]; setenv bootvar recoveryboot;fi

Recovery routine for battery charging

Load recovery with mmcblk0p1:

/charging.zip

.

[B]bcbcharging=[/B]if fatload mmc ${mmcromdev}:2 0x81c00000 [U]BCB[/U] 0x1000; then mw.b 0x81c00000 0 0x200; cp.s "boot-recovery" 0x81c00000 0xd; cp.s "[U]recovery --update_package=BOOT:charging.zip[/U]" 0x81c00040 0x2c; mw.b 0x81c00048 0xa 1; fatsave mmc ${mmcromdev}:2 0x81c00000 [U]BCB[/U] 0x200;fi

Recovery routine for battery reset

Rewrite mmcblk0p2:/

BCB

and load

recovery --wipe_data_ui

.

[B]bcbreset=i[/B]f fatload mmc ${mmcromdev}:2 0x81c00000 [U]BCB[/U] 0x1000; then mw.b 0x81c00000 0 0x200; cp.s "boot-recovery" 0x81c00000 0xd; cp.s "[U]recovery --wipe_data_ui[/U]" 0x81c00040 0x19; mw.b 0x81c00048 0xa 1; fatsave mmc ${mmcromdev}:2 0x81c00000 [U]BCB[/U] 0x200;fi

Actual boot

Normal mode

Increase boot count in mmcblk0p2:

/devconf/BootCnt

, load

uImage

and

uRamdisk

.

[B]normalboot=[/B]echo [I]Booting into Android[/I];if fatload mmc ${mmcromdev}:2 0x81c00000 [U]devconf/BootCnt[/U] 4; then calc.l *0x81c00000 + 1 0x81c00000; fatsave mmc ${mmcromdev}:2 0x81c00000 [U]devconf/BootCnt[/U] 4;fi;run [B]setbootargs[/B];mmcinit $mmcbootdev; fatload mmc $mmcbootdev ${loadaddr} [U]uImage[/U]; fatload mmc $mmcbootdev ${loadaddrrd} [U]uRamdisk[/U]; bootm ${loadaddr} ${loadaddrrd}

Recovery mode

Load

uRecImg

and

uRecRam

(do not increase boot count).

[B]recoveryboot=[/B]echo [I]Booting into recovery mode[/I];run [B]setbootargs[/B];mmcinit $mmcbootdev; fatload mmc $mmcbootdev ${loadaddr} [U]uRecImg[/U]; fatload mmc $mmcbootdev ${loadaddrrd} [U]uRecRam[/U]; bootm ${loadaddr} ${loadaddrrd}