WOL on nwsp2

Post Reply
fvdw
Posts: 777
Joined: Wed Mar 18, 2009 11:13 pm

WOL on nwsp2

Post by fvdw » Wed Dec 14, 2011 10:22 pm

One of the last secrets on the nwsp2 that i could not crack yet is the implementation of WOL on the nwsp2
I want to know it and to use it in combination with my own compiled kernel and firmware.

From information on other lacie devices it seems that some have a setting that makes Uboot wait for a magic packet to continue boot. Does someone knows if this is also the case on the nwsp2 ?
There is a binary in the lacie firmware (sleepmode_config) that has various options you can set, one of them is enabling WOL. In the Python scripts used by unicorn you can find that unicorn uses this binary to enter sleep mode and enable disable WOL. This program seems to make changes in flash as it can also be used to influence the behaviour when you connect the power to the nwsp2. Normally you need to push the button on the back for 2 seconds when connecting the power. When you set power on with sleepmode_config. It will boot without the need to press that button.
So I expected that it also would set something in flash to tell Uboot to wait yes/no for the magic packet.
This should be visible in the output from the serial console during booting. In the wiki of the nwsp2 there is an output posted from the serial console but it does not mention that it waits for the magic packet. However probably this will only appear when the nwsp2 is set WOL enabled. Possible the output was made with a nwsp2 without WOL enabled.
Did anyone made a save of the serial console output with WOL enabled ? and if then Uboot indeed waits for the magic packet to arrive and continues boot after receiving it ? If it doesn't then they probably fake WOL in software and just switch of power of HDD and USB and the blue led.

I tried several combinations in sleepmode_config (which options you can set you can see by just typing sleepmode_config) but none of them made WOL working after shutting down to system, or halting the system after a reboot and wait for the magic packet.
None of the settings seems to preserve the power on the network card when doing a reboot, halt, or poweroff. I concludes this form the fact that all the leds are out on the NIC. In that case I guess it will never be able to read the magic packet or am I wrong ?

Before implementing a fake wake on lan method it would be nice to know if the original kernel/firmware uses Uboot or not to make WOL working and how it is implemented. If it is done via Uboot this is a nicer solution then faking a WOL in software which also be not equal energy efficient.

ltree
Posts: 27
Joined: Tue Jan 18, 2011 7:44 pm

Re: WOL on nwsp2

Post by ltree » Thu Dec 15, 2011 2:58 am

Looking through the sleepmode_config with a hex editor reveals it potentially writes to the EEPROM at /sys/bus/i2c/devices/0-0050/eeprom , which correlates with the specified values (namely "Fake sleepmode") for the EEPROM here. I don't have the stock uBoot or FW anymore so I can't test if changing the bit in the EEPROM will allow it to listen for the WOL packet (you may need a special kernel module for the I2C device to be available on that path in your non-stock firmware).

Also, whilst searching I came across this implementation of a fake wake on lan for another Lacie device which could save you some time if it comes to that solution: http://www.rigacci.org/wiki/doku.php/do ... ake-on-lan . That page also suggests that uBoot isn't doing the WOL listening but it is happening in the boot process instead (which again correlates with the EEPROM table linked above that says fake sleepmode is implemented in the initramfs rather than uBoot)

Finally, some vaguely relevant discussion: viewtopic.php?f=236&t=2386&start=30

fvdw
Posts: 777
Joined: Wed Mar 18, 2009 11:13 pm

Re: WOL on nwsp2

Post by fvdw » Thu Dec 15, 2011 5:50 pm

thanks for this is info ltree, much appreciated. I also found this "fake on lan" info and it is a way to implement it. But because this sleepmode_config seems to be the utility used in the stock firmware I thought maybe there is more easy way. As said this sleepmode_config is working and I can make changes such as the behavior of power up when connecting main power.
But when enabling wol and sending a magic packet nothing happens. So there is still something missing.
Its not a big issue but a nice extra feature if it would work

fvdw
Posts: 777
Joined: Wed Mar 18, 2009 11:13 pm

Re: WOL on nwsp2

Post by fvdw » Thu Dec 15, 2011 6:14 pm

I made some command with sleepmode_config and indeed you can see the effect in the eeprom data.
I think the way it works is that the kernel is loaded and it is reading the content of the eeprom and then waits of the wol flag is set.
For the wakeup schedule at specific times it seems to use also data that you can store in eeprom with the sleepmode_config command to start a cron job.
But uboot is also doing something with the data in this eeprom because dependent on the power bit it will change the way it will power up after connection the main power.
Last edited by fvdw on Thu Dec 15, 2011 7:41 pm, edited 1 time in total.

Mijzelf
Posts: 6251
Joined: Mon Jun 16, 2008 10:45 am

Re: WOL on nwsp2

Post by Mijzelf » Thu Dec 15, 2011 7:38 pm

The initramfs of the NS2-Lite you gave me, contains this snippet:

Code: Select all

       # sleepmode
        ifconfig eth0 up
        /sbin/hwclock -s
        /sbin/ipcommd -m sleep &
        l_ipcommd_pid=$!
        /sbin/sleepmode --poweroff

        # Check for 'ipcommd' server's PID. Wait up to 3s for its PID file...
        l_ipcommd_pid_tries=3
        while true; do
            l_ipcommd_pid_tries=`expr $l_ipcommd_pid_tries - 1`
            l_ipcommd_run_pid=`cat "$ipcommd_pid_file" 2> /dev/null`
            if [ -n "$l_ipcommd_run_pid" -o $l_ipcommd_pid_tries -eq 0 ]; then
                [ -n "$l_ipcommd_run_pid" ] && l_ipcommd_pid=$l_ipcommd_run_pid
                break
            fi

            sleep 1
        done

        # Stop 'ipcommd' server
        kill -9 $l_ipcommd_pid

        waitForDevices 1 1
        firstDisk="`getAvailableDisks | head -n 1`"
So I suppose ipcommd is responsible for the waiting for WOL, while sleepmode is responsible for the low powerstate of the rest of the NAS.

fvdw
Posts: 777
Joined: Wed Mar 18, 2009 11:13 pm

Re: WOL on nwsp2

Post by fvdw » Thu Dec 15, 2011 7:53 pm

mmm..interesting, another part of the puzzle

unfortunate when wanting to use ipcommd you get this

Code: Select all

/$ /ipcommd --help
/ipcommd: error while loading shared libraries: libunicorn.so: cannot open shared object file: No such file or directory
so its related to unicorn and and I don't like unicorn :lol:
Later this evening I will try what happens if I copy this shared object library, now I have some visitors asking for my attention

ps interesting to see that the lite uses sleepmode and sleepmode_config (nwsp2 classic) also --poweroff is not an option in sleepmode_config So the liet firmware contains a newer version ?? need to check that out as well

Mijzelf
Posts: 6251
Joined: Mon Jun 16, 2008 10:45 am

Re: WOL on nwsp2

Post by Mijzelf » Thu Dec 15, 2011 8:17 pm

I think you should look at the binaries in initramfs:

Code: Select all

file ipcommd sleepmode 
ipcommd:   ELF 32-bit LSB executable, ARM, version 1, statically linked, stripped
sleepmode: ELF 32-bit LSB executable, ARM, version 1, statically linked, stripped
So no dependencies.

fvdw
Posts: 777
Joined: Wed Mar 18, 2009 11:13 pm

Re: WOL on nwsp2

Post by fvdw » Thu Dec 15, 2011 10:04 pm

yes of course you are right, I overlooked that, these binaries in initramfs are different

fvdw
Posts: 777
Joined: Wed Mar 18, 2009 11:13 pm

Re: WOL on nwsp2

Post by fvdw » Fri Dec 16, 2011 11:32 am

extracting the initramfs is a little more complicated then I expected as it is part of the lacie kernel file. I never extracted it from a kernel so I need some help here.
I use a script found on the internet to extract initramfs from zImage (can it also be done from uImage ?). Using that script gives a cpio archive with name initramfs.cpio. Now this has to be extracted using cpio command and there is were I struggle as the use of this command is rather complicated at least for me :lol:
cpio everytime overwrite my root file system if it extracts the initramfs.cpio archive. I used cpio -idv < /path-to/initramfs.cpio and executed the command in a new created folder (mkdir output and cd output). According the information I found on cpio this should extract it in the directory were you execute the command (in this case output), but as said it doesn't do that it extracts it in the root of the file system overwritting existing files. What am I doing wrong here ? and how to get it in a separate folder

Mijzelf
Posts: 6251
Joined: Mon Jun 16, 2008 10:45 am

Re: WOL on nwsp2

Post by Mijzelf » Fri Dec 16, 2011 12:09 pm

This is the way I extracted the initramfs:

Code: Select all

cat nwsp2-lite-sda6-image.img.gz | gzip -dc >uImage
tail -c +12569 uImage | gzip -dc >kernel
tail -c +89505 kernel | gzip -dc >initramfs
mkdir initramfs.d
cd initramfs.d
cpio --no-absolute-filenames -i <../initramfs
cd ..
I used binwalk to find the offsets of the kernel in uImage and the offset of initramfs in the kernel. (The offset is 1 less than the used parameter, due to the exact meaning of 'tail -c +N').
fvdw wrote:I use a script found on the internet to extract initramfs from zImage (can it also be done from uImage ?)
Depends on how the script works. a uImage is just a zImage with a 64 byte header. So if it just searches for the start of the gzipped kernel (like binwalk does) it doesn't matter.

fvdw
Posts: 777
Joined: Wed Mar 18, 2009 11:13 pm

Re: WOL on nwsp2

Post by fvdw » Fri Dec 16, 2011 10:45 pm

this is the script I used

Code: Select all

  #!/bin/bash
   zImage=$1
   #========================================================
   # find start of gziped kernel object in the zImage file:
   #========================================================
   pos=`grep -P -a -b -m 1 --only-matching $'\x1F\x8B\x08' $zImage | cut -f 1 -d :`
   echo "-I- Extracting kernel image from $zImage (start = $pos)"
   
   #========================================================================
   # the cpio archive might be gzipped too, so two gunzips could be needed:
   #========================================================================
   dd if=$zImage bs=1 skip=$pos | gunzip > /tmp/kernel.img
   pos=`grep -P -a -b -m 1 --only-matching $'\x1F\x8B\x08' /tmp/kernel.img | cut -f 1 -d :`
   #===========================================================================
   # find start and end of the "cpio" initramfs image inside the kernel object:
   # ASCII cpio header starts with '070701'
   # The end of the cpio archive is marked with an empty file named TRAILER!!!
   #===========================================================================
   if [ ! $pos = "" ]; then
       echo "-I- Extracting compressed cpio image from kernel image (start = $pos)"
       dd if=/tmp/kernel.img bs=1 skip=$pos | gunzip > /tmp/cpio.img
       start=`grep -a -b -m 1 --only-matching '070701' /tmp/cpio.img | head -1 | cut -f 1 -d :`
       end=`grep -a -b -m 1 --only-matching 'TRAILER!!!' /tmp/cpio.img | head -1 | cut -f 1 -d :`
       inputfile=/tmp/cpio.img
   else
       echo "-I- Already uncompressed cpio.img, not decompressing"
       start=`grep -a -b -m 1 --only-matching '070701' /tmp/kernel.img | head -1 | cut -f 1 -d :`
       end=`grep -a -b -m 1 --only-matching 'TRAILER!!!' /tmp/kernel.img | head -1 | cut -f 1 -d :`
       inputfile=/tmp/kernel.img
   fi
   
   # 11 bytes = length of TRAILER!!! zero terminated string, fixes premature end of file warning in CPIO
   end=$((end + 11))
   count=$((end - start))
   if (($count < 0)); then
       echo "-E- Couldn't match start/end of the initramfs image."
       exit
   fi
   echo "-I- Extracting initramfs image from $inputfile (start = $start, end = $end)"
   dd if=$inputfile bs=1 skip=$start count=$count > initramfs.cpio
   
So it search indeed for start and end of initramfs cpio archive and then uses dd to copy that part from zImage
it gives as output file with name initramdfs.cpio

Unfortunate my cpio is a busybox version that don't support --no-absolute-filenames option
I used then a trick to run busybox in a chrooted environment and let it extract the archive in the chroot
of course i could compile cpio full version but this was quicker :)

fvdw
Posts: 777
Joined: Wed Mar 18, 2009 11:13 pm

Re: WOL on nwsp2

Post by fvdw » Sat Dec 17, 2011 2:25 pm

this wat ipcommd is capable of (the version from initramfs)

Code: Select all

 # ./ipcommd -h
version 2.3
usage: ./ipcommd [options]
options:
        -h              print this help
        -v              print version
        -f              run in foreground (instead of background daemon by default)
        -c              log to console
        -m <mode>       {normal, sleep, rescue, not-configurable}
        -n <host name>
        -p <product name>
        -i <product ID>
        -w <hardware ID>
        -r <hardware revision>
        -s <system version>
I think it is server listening for some commands

The "sleepmode" command from initramfs doesn't have a help text
if you executed the command while you have set sleepmode to disabled using sleepmode-config then it outputs this regardless which option you give (and indeed the blue led goes out)

Code: Select all

 # ./sleepmode -h
Sleep mode disabled
Powering off leds
Find and update brightness
Powering on leds : resume
Find and update hd_blink
Find and update brightness
Find and update trigger
Find and update delay_on
Find and update delay_off
Find and update hd_blink
When you have set sleepmode enabled with sleepmode-config then it doesn't return and remains in memory
if you then use sleepmode --poweroff you get this

Code: Select all

 # ./sleepmode --poweroff
Powering off leds
Find and update brightness
WARNING: no fan control sys file!!
Powering off fans
WARNING: no fan control sys file!!
ERROR: Powering off peripherals
Powering off harddisk
ERROR: opening /sys/devices/platform/gpio-hd-power/power
ERROR: Powering off peripherals
so it reads the eeprom bit to see if sleepmode is enabled or not and dependent on that it acts
and indeed it (tries) to poweroff the fan and harddisk, of course it fails on my system I use my own kernel
..a fake sleepmode as the processor will be kept running, also NIC (logical otherwise wol can not work) and USB are kept powered

because this command is give at a moment during boot that not yet any daemon is running the box will not awake and probably wait that some press the power button or a magic packet arrives on the NIC

Only need to figure out if I can use ipcommd for listening on the NIC for the magic packet

for those interested this is the part of the init script used in intramfs to enter and wake from sleep mode (as Mijzelf already indicated)
There is also a command "WaitForDevices 1 1" never heard of that before, is it a standard linux command build in in the kernels?

Code: Select all

       # sleepmode
        ifconfig eth0 up
        /sbin/hwclock -s
	/sbin/ipcommd -m sleep &
	l_ipcommd_pid=$!
        /sbin/sleepmode --poweroff

        # Check for 'ipcommd' server's PID. Wait up to 3s for its PID file...
        l_ipcommd_pid_tries=3
        while true; do
            l_ipcommd_pid_tries=`expr $l_ipcommd_pid_tries - 1`
            l_ipcommd_run_pid=`cat "$ipcommd_pid_file" 2> /dev/null`
            if [ -n "$l_ipcommd_run_pid" -o $l_ipcommd_pid_tries -eq 0 ]; then
                [ -n "$l_ipcommd_run_pid" ] && l_ipcommd_pid=$l_ipcommd_run_pid
                break
            fi

            sleep 1
        done

        # Stop 'ipcommd' server
	kill -9 $l_ipcommd_pid

        waitForDevices 1 1
        firstDisk="`getAvailableDisks | head -n 1`"

Post Reply