Contents

Wake on Lan and Dual Boot

Introduction

Wake on Lan is a technology delvelopped in 1995, that allows you to start a computer by sending a magic packet (usually in udp). I use it since a few years and it is especially useful when you are in a remote place, you can just start your computer and connect to it through rdp. The only problem is that this approach is not really suitable for dual-boot setups.

The Problem

Sometimes, I need to run linux process that are using the GPU, so until the gpu support is added to the WSL2, a dual-boot is mandatory for me. However, a magic packet allows you to start you computer but you have no way to interact with it until the os is fully started. So it’s impossible to access boot menu using wake on lan.

The Solution

So, if it’s impossible to do it in software, let’s use some hardware ! Our setup will be very simple: an Arduino Leonardo and an Ethernet Shield. Why a leonardo and not a uno or a nano ? Because, the Arduino Leonardo have a different kind of usb controller that allows to emulate an HID device, especially a keyboard in our case.

/2020/10/06/wake-on-lan-and-dual-boot/assets/arduino.jpg
Arduino Leonardo with Ethernet shield

The idea is to connect an ethernet shield to this arduino, so that we can send an http request with the os to start. The arduino will then send a magic packet (which is really simple to create as it is composed of 6x “FF” and 16x the mac address of the computer), wait for a predetermined amount of time (time to show the os selection screen), and then emulate the up/down/enter keys to select the correct os.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include <SPI.h>
#include <Ethernet.h>
#include "Keyboard.h"

#define bootDelay 9000 //time to get the boot menu from wol, in ms
#define keyPressDelay 100 //in ms
#define resetDelay 4

byte mac[] = {
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};

int wolPort = 9;
byte remote_MAC_ADD[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
byte broadCastIp[] = { 255, 255, 255, 255 };

IPAddress ip(192, 168, 1, 5);
EthernetServer server(80);

void setup() 
{
  Keyboard.begin();

  Ethernet.begin(mac, ip);

  if (Ethernet.hardwareStatus() == EthernetNoHardware || Ethernet.linkStatus() == LinkOFF) 
  {
    return;
  }
  server.begin();
}


void loop() 
{
  EthernetClient client = server.available();
  if (client) 
  {
    // an http request ends with a blank line
    String message = "";
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) 
      {
        char c = client.read();
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) 
        {
          String param = message.substring(5, message.indexOf("HTTP")-1);
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Access-Control-Allow-Origin: *");
          if(param == "")
          {
            client.println("Content-Type: text/html");
            client.println("Connection: close");
            client.println();
            client.print(F(\
              "<html>\
                <head>\
                    <title>Wake On Lan</title>\
                    <meta name='viewport' content='width=device-width, initial-scale=1'>\
                    <style>html{background-color:#222222;}.pure-button{display:inline-block;line-height:normal;white-space:nowrap;vertical-align:middle;text-align:center;cursor:pointer;-webkit-user-drag:none;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;-webkit-box-sizing:border-box;box-sizing:border-box}.pure-button::-moz-focus-inner{padding:0;border:0}.pure-button-group{letter-spacing:-.31em;text-rendering:optimizespeed}.opera-only :-o-prefocus,.pure-button-group{word-spacing:-.43em}.pure-button-group .pure-button{letter-spacing:normal;word-spacing:normal;vertical-align:top;text-rendering:auto}.pure-button{font-family:inherit;font-size:100%;padding:.5em 1em;color:rgba(0,0,0,.8);border:none transparent;background-color:#e6e6e6;text-decoration:none;border-radius:2px}.pure-button-hover,.pure-button:focus,.pure-button:hover{background-image:-webkit-gradient(linear,left top,left bottom,from(transparent),color-stop(40%,rgba(0,0,0,.05)),to(rgba(0,0,0,.1)));background-image:linear-gradient(transparent,rgba(0,0,0,.05) 40%,rgba(0,0,0,.1))}.pure-button:focus{outline:0}.pure-button-active,.pure-button:active{-webkit-box-shadow:0 0 0 1px rgba(0,0,0,.15) inset,0 0 6px rgba(0,0,0,.2) inset;box-shadow:0 0 0 1px rgba(0,0,0,.15) inset,0 0 6px rgba(0,0,0,.2) inset;border-color:#000}.pure-button-disabled,.pure-button-disabled:active,.pure-button-disabled:focus,.pure-button-disabled:hover,.pure-button[disabled]{border:none;background-image:none;opacity:.4;cursor:not-allowed;-webkit-box-shadow:none;box-shadow:none;pointer-events:none}.pure-button-hidden{display:none}.pure-button-primary,.pure-button-selected,a.pure-button-primary,a.pure-button-selected{background-color:#0078e7;color:#fff}.pure-button-group .pure-button{margin:0;border-radius:0;border-right:1px solid rgba(0,0,0,.2)}.pure-button-group .pure-button:first-child{border-top-left-radius:2px;border-bottom-left-radius:2px}.pure-button-group .pure-button:last-child{border-top-right-radius:2px;border-bottom-right-radius:2px;border-right:none}.button-error,.button-secondary,.button-success,.button-warning{color:#fff;border-radius:4px;text-shadow:0 1px 1px rgba(0,0,0,.2)}.button-success{background:#1cb841}.button-error{background:#ca3c3c}.button-warning{background:#df7514}</style>\
                </head>\
                <body>\
                  <br><a class='button-success pure-button' onclick='redir(\"/0/on\")'>Start Windows</a>&nbsp;&nbsp;\
                  <a class='button-error pure-button' onclick='redir(\"/0/off\")'>Stop Windows</a><br><br>\
                  <a class='button-success pure-button' onclick='redir(\"/1/on\")'>Start Linux</a>&nbsp;&nbsp;\
                  <a class='button-error pure-button' onclick='redir(\"/1/off\")'>Stop Linux</a><br><br>\
                  <a class='button-warning pure-button' onclick='redir(\"/lock\")'>Lock</a>\
                </body>\
                <script>function redir(path) { window.location.href = window.location.href + path }</script>\
              </html>")
            );
          }
          else
          {
            client.println("Content-Type: application/json");
            client.println("Connection: close");
            client.println();
            client.print("{\"command\":\"");
            client.print(param);
            client.print("\",\"response\":\"");
            client.print(doAction(param));
            client.println("\"}");
          }
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
          
        } else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
        message += c;
      }
    }
    delay(1);
    client.stop();
  }

}

//========================================================= WOL functions =========================

void sendWol()
{
    byte magicPacket[102];
    int i,c1,j=0;
   
    for(i = 0; i < 6; i++,j++){
        magicPacket[j] = 0xFF;
    }
    for(i = 0; i < 16; i++){
        for( c1 = 0; c1 < 6; c1++,j++)
          magicPacket[j] = remote_MAC_ADD[c1];
    }
    
    EthernetUDP Udp;
    Udp.begin(wolPort);
    Udp.beginPacket(broadCastIp, wolPort);
    Udp.write(magicPacket, sizeof magicPacket);
    Udp.endPacket();
}

//========================================================= Actions =============================================

String doAction(String param)
{
  //0 = Windows, 1 = Ubuntu
  
  if (param == "0/on")
  {
    sendWol();
    delay(bootDelay);
    Keyboard.println(); //enter
  }
  else if(param == "0/off")
  {
    Keyboard.press(KEY_LEFT_GUI);
    Keyboard.press('d');
    delay(keyPressDelay);
    Keyboard.releaseAll(); // Windows + D
    
    delay(keyPressDelay);
    
    Keyboard.press(KEY_LEFT_ALT);
    Keyboard.press(KEY_F4);
    delay(keyPressDelay);
    Keyboard.releaseAll(); // Alt + F4
    
    delay(keyPressDelay);
    
    Keyboard.println(); //enter

    delay(1000);
    // if the sequence failed (the computer is likely locked), continue with a sequence to poweroff from the lockscreen
    // enter -> 4 tab -> enter -> down -> enter -> enter
    
    for(char i = 0; i<4; i++) {
      Keyboard.press(KEY_TAB); //tab
      delay(keyPressDelay);
      Keyboard.releaseAll();
    }
    Keyboard.println(); //enter
    delay(keyPressDelay);
    
    Keyboard.press(KEY_DOWN_ARROW); //down
    delay(keyPressDelay);
    Keyboard.releaseAll();
    
    Keyboard.println(); //enter
    delay(keyPressDelay);
    Keyboard.println(); //enter
    delay(keyPressDelay);  
  }
  else if(param == "1/on")
  {
    sendWol();
    delay(bootDelay);
    Keyboard.press(KEY_DOWN_ARROW);
    delay(keyPressDelay);
    Keyboard.releaseAll();
    
    delay(keyPressDelay);
    
    Keyboard.println(); //enter
  }
  else if(param == "1/off")
  {
    Keyboard.press(KEY_LEFT_CTRL);
    Keyboard.press(KEY_LEFT_ALT);
    Keyboard.press('s');
    delay(keyPressDelay);
    Keyboard.releaseAll(); // Ctrl + Alt + S
  }
  else if(param == "lock")
  {
    Keyboard.press(KEY_LEFT_GUI);
    Keyboard.press('l');
    delay(keyPressDelay);
    Keyboard.releaseAll(); // Win + L
  }
  else if(param == "status")
  {
    Serial.begin(9600);
    if (Serial)
      return "true";
    else
      return "false";
    Serial.end();
  }
  else
  {
    return "error";
  }
  
  return "ok";
}

With this simple code, the arduino will start an http server on port 80, and listen for simple commands like 2/start to start linux of 1/stop to stop windows, we can even serve a very simple ui on the default page.

I could have stopped there, but I decided to create a simple app for my phone, to easily wake-up the computer. For this really quick project, I used Apache Cordova (though I don’t recommand using it for real projects) and bootstrap. And a few lines of html later, there is our app !

Mobile App
Web UI

Conclusion

So with less than $50, you can create a Wake On Lan device that supports multi-os systems. You could even bring wake up support to an incompatible computer using a realy or an optocoupler to “manually push the button”.