[release] Update version to 0.9.5 for release
[gpxe.git] / contrib / wakeonlan / wol.c
blob40a8415a0d36ceee99646ed542df941021bda0f7
1 /*
2 * Copyright (C) 2008 William Stewart.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 #include <stdio.h>
19 #include <stdint.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <errno.h>
23 #include <byteswap.h>
24 #include <gpxe/features.h>
25 #include <gpxe/netdevice.h>
26 #include <gpxe/if_ether.h>
27 #include <gpxe/iobuf.h>
28 #include <usr/ifmgmt.h>
29 #include <usr/wol.h>
30 #include <timer.h>
32 /** @file
34 * Wake on lan
38 /**
39 * Boot from a network device
41 * @v netdev Network device
42 * @ret rc Return status code
44 #define WOL_MSG_LEN (6 + 16*6)
46 void wakeup_server(char *server_adr)
48 int rc, i,j;
49 unsigned char *buf;
50 uint8_t eth_broadcast[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
51 unsigned int len;
52 struct io_buffer *iobuf;
53 struct ethhdr *ethhdr;
54 struct net_device *netdev;
56 for_each_netdev ( netdev ) {
57 break;
60 if (netdev == NULL)
62 printf("Could not find netdev\n");
63 return;
66 /* Open device and display device status */
67 if ( (ifopen ( netdev ) ) != 0 )
69 printf("Could not open netdev\n");
70 return;
73 /* Create outgoing I/O buffer */
74 iobuf = alloc_iob ((ETH_HLEN + WOL_MSG_LEN)*2);
75 if (!iobuf)
77 printf("Could not allocate iob\n");
78 return;
81 ethhdr = iob_put(iobuf, sizeof(*ethhdr));
83 /* Build Ethernet header */
84 memcpy (ethhdr->h_dest, eth_broadcast, ETH_ALEN );
85 memcpy (ethhdr->h_source, netdev->ll_addr, ETH_ALEN );
86 ethhdr->h_protocol = htons (0x0842);
88 buf = iob_put (iobuf, WOL_MSG_LEN);
90 /* Build the message to send - 6 x 0xff then 16 x dest address */
91 len =0;
92 for (i = 0; i < 6; i++)
93 buf[len++] = 0xff;
94 for (j = 0; j < 16; j++)
95 for (i = 0; i < 6; i++)
96 buf[len++] = server_adr[i];
98 rc = netdev_tx (netdev, iobuf);
100 if (rc !=0)
101 printf("Failed to transmit WOL packet\n");
103 /* Give the controller a chance to send it before checking */
104 mdelay(100);
106 netdev_poll(netdev);