2 * Copyright (c) 2001 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
25 * 14 December, 2001 Dieter Siegmund (dieter@apple.com)
28 #include <sys/param.h>
29 #include <sys/systm.h>
30 #include <sys/kernel.h>
32 #include <sys/ioctl.h>
34 #include <sys/mount.h>
36 #include <sys/filedesc.h>
37 #include <sys/vnode.h>
38 #include <sys/malloc.h>
39 #include <sys/socket.h>
40 #include <sys/reboot.h>
42 #include <net/if_dl.h>
43 #include <net/if_types.h>
44 #include <net/route.h>
45 #include <netinet/in.h>
46 #include <netinet/if_ether.h>
47 #include <netinet/dhcp_options.h>
50 //#include <libkern/libkern.h>
52 extern dev_t rootdev
; /* device of the root */
53 extern struct filedesc filedesc0
;
55 extern char * strchr(const char *str
, int ch
);
57 extern int nfs_mountroot(); /* nfs_vfsops.c */
58 extern int (*mountroot
)(void);
60 extern unsigned char rootdevice
[];
62 static int S_netboot
= 0;
63 static struct netboot_info
* S_netboot_info_p
;
66 IOBSDRegistryEntryForDeviceTree(char * path
);
69 IOBSDRegistryEntryRelease(void * entry
);
72 IOBSDRegistryEntryGetData(void * entry
, char * property_name
,
75 extern int vndevice_root_image(const char * path
, char devname
[],
77 extern int di_root_image(const char *path
, char devname
[], dev_t
*dev_p
);
80 static boolean_t path_getfile
__P((char * image_path
,
81 struct sockaddr_in
* sin_p
,
82 char * serv_name
, char * pathname
));
84 #define BOOTP_RESPONSE "bootp-response"
85 #define BSDP_RESPONSE "bsdp-response"
86 #define DHCP_RESPONSE "dhcp-response"
89 bootp(struct ifnet
* ifp
, struct in_addr
* iaddr_p
, int max_retry
,
90 struct in_addr
* netmask_p
, struct in_addr
* router_p
,
93 #define IP_FORMAT "%d.%d.%d.%d"
94 #define IP_CH(ip) ((u_char *)ip)
95 #define IP_LIST(ip) IP_CH(ip)[0],IP_CH(ip)[1],IP_CH(ip)[2],IP_CH(ip)[3]
97 #define kNetBootRootPathPrefixNFS "nfs:"
98 #define kNetBootRootPathPrefixHTTP "http:"
101 kNetBootImageTypeUnknown
= 0,
102 kNetBootImageTypeNFS
= 1,
103 kNetBootImageTypeHTTP
= 2,
106 struct netboot_info
{
107 struct in_addr client_ip
;
108 struct in_addr server_ip
;
110 int server_name_length
;
112 int mount_point_length
;
114 int image_path_length
;
115 NetBootImageType image_type
;
120 inet_aton(char * cp
, struct in_addr
* pin
)
122 u_char
* b
= (char *)pin
;
126 for (p
= cp
, i
= 0; i
< 4; i
++) {
127 u_long l
= strtoul(p
, 0, 0);
132 if (i
< 3 && p
== NULL
)
140 * Function: parse_booter_path
142 * Parse a string of the form:
143 * "<IP>:<host>:<mount>[:<image_path>]"
144 * into the given ip address, host, mount point, and optionally, image_path.
147 * The passed in string is modified i.e. ':' is replaced by '\0'.
149 * "17.202.16.17:seaport:/release/.images/Image9/CurrentHera"
151 static __inline__ boolean_t
152 parse_booter_path(char * path
, struct in_addr
* iaddr_p
, char * * host
,
153 char * * mount_dir
, char * * image_path
)
160 colon
= strchr(start
, ':');
165 if (inet_aton(start
, iaddr_p
) != 1) {
171 colon
= strchr(start
, ':');
180 colon
= strchr(start
, ':');
195 * Function: find_colon
197 * Find the next unescaped instance of the colon character.
198 * If a colon is escaped (preceded by a backslash '\' character),
199 * shift the string over by one character to overwrite the backslash.
201 static __inline__
char *
202 find_colon(char * str
)
207 while ((colon
= strchr(start
, ':')) != NULL
) {
211 if (colon
== start
) {
214 if (colon
[-1] != '\\')
216 for (dst
= colon
- 1, src
= colon
; *dst
!= '\0'; dst
++, src
++) {
225 * Function: parse_netboot_path
227 * Parse a string of the form:
228 * "nfs:<IP>:<mount>[:<image_path>]"
229 * into the given ip address, host, mount point, and optionally, image_path.
231 * - the passed in string is modified i.e. ':' is replaced by '\0'
232 * - literal colons must be escaped with a backslash
235 * nfs:17.202.42.112:/Library/NetBoot/NetBootSP0:Jaguar/Jaguar.dmg
236 * nfs:17.202.42.112:/Volumes/Foo\:/Library/NetBoot/NetBootSP0:Jaguar/Jaguar.dmg
238 static __inline__ boolean_t
239 parse_netboot_path(char * path
, struct in_addr
* iaddr_p
, char * * host
,
240 char * * mount_dir
, char * * image_path
)
245 if (strncmp(path
, kNetBootRootPathPrefixNFS
,
246 strlen(kNetBootRootPathPrefixNFS
)) != 0) {
251 start
= path
+ strlen(kNetBootRootPathPrefixNFS
);
252 colon
= strchr(start
, ':');
257 if (inet_aton(start
, iaddr_p
) != 1) {
263 colon
= find_colon(start
);
272 (void)find_colon(start
);
275 *host
= inet_ntoa(*iaddr_p
);
280 parse_image_path(char * path
, struct in_addr
* iaddr_p
, char * * host
,
281 char * * mount_dir
, char * * image_path
)
283 if (path
[0] >= '0' && path
[0] <= '9') {
284 return (parse_booter_path(path
, iaddr_p
, host
, mount_dir
,
287 return (parse_netboot_path(path
, iaddr_p
, host
, mount_dir
,
292 get_root_path(char * root_path
)
295 boolean_t found
= FALSE
;
299 entry
= IOBSDRegistryEntryForDeviceTree("/chosen");
303 pkt
= IOBSDRegistryEntryGetData(entry
, BSDP_RESPONSE
, &pkt_len
);
304 if (pkt
!= NULL
&& pkt_len
>= sizeof(struct dhcp
)) {
305 printf("netboot: retrieving root path from BSDP response\n");
308 pkt
= IOBSDRegistryEntryGetData(entry
, BOOTP_RESPONSE
,
310 if (pkt
!= NULL
&& pkt_len
>= sizeof(struct dhcp
)) {
311 printf("netboot: retrieving root path from BOOTP response\n");
320 reply
= (struct dhcp
*)pkt
;
321 (void)dhcpol_parse_packet(&options
, reply
, pkt_len
, NULL
);
323 path
= (char *)dhcpol_find(&options
,
324 dhcptag_root_path_e
, &len
, NULL
);
326 bcopy(path
, root_path
, len
);
327 root_path
[len
] = '\0';
331 IOBSDRegistryEntryRelease(entry
);
336 static struct netboot_info
*
337 netboot_info_init(struct in_addr iaddr
)
339 struct netboot_info
* info
;
340 char * root_path
= NULL
;
341 boolean_t use_hdix
= TRUE
;
342 char * vndevice
= NULL
;
344 MALLOC_ZONE(vndevice
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
345 if (PE_parse_boot_arg("vndevice", vndevice
) == TRUE
) {
348 _FREE_ZONE(vndevice
, MAXPATHLEN
, M_NAMEI
);
350 info
= (struct netboot_info
*)kalloc(sizeof(*info
));
351 bzero(info
, sizeof(*info
));
352 info
->client_ip
= iaddr
;
353 info
->image_type
= kNetBootImageTypeUnknown
;
354 info
->use_hdix
= use_hdix
;
356 /* check for a booter-specified path then a NetBoot path */
357 MALLOC_ZONE(root_path
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
358 if (PE_parse_boot_arg("rp", root_path
) == TRUE
359 || PE_parse_boot_arg("rootpath", root_path
) == TRUE
360 || get_root_path(root_path
) == TRUE
) {
361 char * server_name
= NULL
;
362 char * mount_point
= NULL
;
363 char * image_path
= NULL
;
364 struct in_addr server_ip
;
366 if (parse_image_path(root_path
, &server_ip
, &server_name
,
367 &mount_point
, &image_path
)) {
368 info
->image_type
= kNetBootImageTypeNFS
;
369 info
->server_ip
= server_ip
;
370 info
->server_name_length
= strlen(server_name
) + 1;
371 info
->server_name
= (char *)kalloc(info
->server_name_length
);
372 info
->mount_point_length
= strlen(mount_point
) + 1;
373 info
->mount_point
= (char *)kalloc(info
->mount_point_length
);
374 strcpy(info
->server_name
, server_name
);
375 strcpy(info
->mount_point
, mount_point
);
377 printf("Server %s Mount %s",
378 server_name
, info
->mount_point
);
379 if (image_path
!= NULL
) {
380 boolean_t needs_slash
;
382 info
->image_path_length
= strlen(image_path
) + 1;
383 if (image_path
[0] != '/') {
385 info
->image_path_length
++;
387 info
->image_path
= (char *)kalloc(info
->image_path_length
);
389 info
->image_path
[0] = '/';
390 strcpy(info
->image_path
+ 1, image_path
);
393 strcpy(info
->image_path
, image_path
);
395 printf(" Image %s", info
->image_path
);
399 else if (strncmp(root_path
, kNetBootRootPathPrefixHTTP
,
400 strlen(kNetBootRootPathPrefixHTTP
)) == 0) {
401 /* only HDIX supports HTTP */
402 info
->image_type
= kNetBootImageTypeHTTP
;
403 info
->use_hdix
= TRUE
;
404 info
->image_path_length
= strlen(root_path
) + 1;
405 info
->image_path
= (char *)kalloc(info
->image_path_length
);
406 strcpy(info
->image_path
, root_path
);
409 printf("netboot: root path uses unrecognized format\n");
412 _FREE_ZONE(root_path
, MAXPATHLEN
, M_NAMEI
);
417 netboot_info_free(struct netboot_info
* * info_p
)
419 struct netboot_info
* info
= *info_p
;
422 if (info
->mount_point
) {
423 kfree(info
->mount_point
, info
->mount_point_length
);
425 if (info
->server_name
) {
426 kfree(info
->server_name
, info
->server_name_length
);
428 if (info
->image_path
) {
429 kfree(info
->image_path
, info
->image_path_length
);
431 kfree(info
, sizeof(*info
));
438 netboot_iaddr(struct in_addr
* iaddr_p
)
440 if (S_netboot_info_p
== NULL
)
443 *iaddr_p
= S_netboot_info_p
->client_ip
;
448 netboot_rootpath(struct in_addr
* server_ip
,
449 char * name
, int name_len
,
450 char * path
, int path_len
)
452 if (S_netboot_info_p
== NULL
)
458 if (S_netboot_info_p
->mount_point_length
== 0) {
461 if (path_len
< S_netboot_info_p
->mount_point_length
) {
462 printf("netboot: path too small %d < %d\n",
463 path_len
, S_netboot_info_p
->mount_point_length
);
466 strcpy(path
, S_netboot_info_p
->mount_point
);
467 strncpy(name
, S_netboot_info_p
->server_name
, name_len
);
468 *server_ip
= S_netboot_info_p
->server_ip
;
474 get_ip_parameters(struct in_addr
* iaddr_p
, struct in_addr
* netmask_p
,
475 struct in_addr
* router_p
)
482 entry
= IOBSDRegistryEntryForDeviceTree("/chosen");
486 pkt
= IOBSDRegistryEntryGetData(entry
, DHCP_RESPONSE
, &pkt_len
);
487 if (pkt
!= NULL
&& pkt_len
>= sizeof(struct dhcp
)) {
488 printf("netboot: retrieving IP information from DHCP response\n");
491 pkt
= IOBSDRegistryEntryGetData(entry
, BOOTP_RESPONSE
, &pkt_len
);
492 if (pkt
!= NULL
&& pkt_len
>= sizeof(struct dhcp
)) {
493 printf("netboot: retrieving IP information from BOOTP response\n");
502 reply
= (struct dhcp
*)pkt
;
503 (void)dhcpol_parse_packet(&options
, reply
, pkt_len
, NULL
);
504 *iaddr_p
= reply
->dp_yiaddr
;
505 ip
= (struct in_addr
*)
506 dhcpol_find(&options
,
507 dhcptag_subnet_mask_e
, &len
, NULL
);
511 ip
= (struct in_addr
*)
512 dhcpol_find(&options
, dhcptag_router_e
, &len
, NULL
);
517 IOBSDRegistryEntryRelease(entry
);
518 return (pkt
!= NULL
);
522 inet_aifaddr(struct socket
* so
, char * name
, const struct in_addr
* addr
,
523 const struct in_addr
* mask
,
524 const struct in_addr
* broadcast
)
526 struct sockaddr blank_sin
= { sizeof(blank_sin
), AF_INET
};
527 struct ifaliasreq ifra
;
529 bzero(&ifra
, sizeof(ifra
));
530 strncpy(ifra
.ifra_name
, name
, sizeof(ifra
.ifra_name
));
532 ifra
.ifra_addr
= blank_sin
;
533 ((struct sockaddr_in
*)&ifra
.ifra_addr
)->sin_addr
= *addr
;
536 ifra
.ifra_mask
= blank_sin
;
537 ((struct sockaddr_in
*)&ifra
.ifra_mask
)->sin_addr
= *mask
;
540 ifra
.ifra_broadaddr
= blank_sin
;
541 ((struct sockaddr_in
*)&ifra
.ifra_broadaddr
)->sin_addr
= *broadcast
;
543 return (ifioctl(so
, SIOCAIFADDR
, (caddr_t
)&ifra
, current_proc()));
547 default_route_add(struct in_addr router
, boolean_t proxy_arp
)
549 struct sockaddr_in dst
;
550 u_long flags
= RTF_UP
| RTF_STATIC
;
551 struct sockaddr_in gw
;
552 struct sockaddr_in mask
;
554 if (proxy_arp
== FALSE
) {
555 flags
|= RTF_GATEWAY
;
559 bzero((caddr_t
)&dst
, sizeof(dst
));
560 dst
.sin_len
= sizeof(dst
);
561 dst
.sin_family
= AF_INET
;
564 bzero((caddr_t
)&gw
, sizeof(gw
));
565 gw
.sin_len
= sizeof(gw
);
566 gw
.sin_family
= AF_INET
;
567 gw
.sin_addr
= router
;
570 bzero(&mask
, sizeof(mask
));
571 mask
.sin_len
= sizeof(mask
);
572 mask
.sin_family
= AF_INET
;
574 printf("netboot: adding default route " IP_FORMAT
"\n",
577 return (rtrequest(RTM_ADD
, (struct sockaddr
*)&dst
, (struct sockaddr
*)&gw
,
578 (struct sockaddr
*)&mask
, flags
, NULL
));
581 static struct ifnet
*
584 struct ifnet
* ifp
= NULL
;
587 ifp
= ifunit(rootdevice
);
590 TAILQ_FOREACH(ifp
, &ifnet
, if_link
)
592 (IFF_LOOPBACK
|IFF_POINTOPOINT
)) == 0)
602 struct in_addr iaddr
= { 0 };
605 struct in_addr netmask
= { 0 };
606 struct proc
* procp
= current_proc();
607 struct in_addr router
= { 0 };
608 struct socket
* so
= NULL
;
610 bzero(&ifr
, sizeof(ifr
));
612 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
614 /* find the interface */
615 ifp
= find_interface();
617 printf("netboot: no suitable interface\n");
621 sprintf(ifr
.ifr_name
, "%s%d", ifp
->if_name
, ifp
->if_unit
);
622 printf("netboot: using network interface '%s'\n", ifr
.ifr_name
);
625 if ((error
= socreate(AF_INET
, &so
, SOCK_DGRAM
, 0)) != 0) {
626 printf("netboot: socreate, error=%d\n", error
);
629 ifr
.ifr_flags
= ifp
->if_flags
| IFF_UP
;
630 error
= ifioctl(so
, SIOCSIFFLAGS
, (caddr_t
)&ifr
, procp
);
632 printf("netboot: SIFFLAGS, error=%d\n", error
);
636 /* grab information from the registry */
637 if (get_ip_parameters(&iaddr
, &netmask
, &router
) == FALSE
) {
638 /* use BOOTP to retrieve IP address, netmask and router */
639 error
= bootp(ifp
, &iaddr
, 32, &netmask
, &router
, procp
);
641 printf("netboot: BOOTP failed %d\n", error
);
645 printf("netboot: IP address " IP_FORMAT
, IP_LIST(&iaddr
));
646 if (netmask
.s_addr
) {
647 printf(" netmask " IP_FORMAT
, IP_LIST(&netmask
));
650 printf(" router " IP_FORMAT
, IP_LIST(&router
));
653 error
= inet_aifaddr(so
, ifr
.ifr_name
, &iaddr
, &netmask
, NULL
);
655 printf("netboot: inet_aifaddr failed, %d\n", error
);
658 if (router
.s_addr
== 0) {
659 /* enable proxy arp if we don't have a router */
660 router
.s_addr
= iaddr
.s_addr
;
662 error
= default_route_add(router
, router
.s_addr
== iaddr
.s_addr
);
664 printf("netboot: default_route_add failed %d\n", error
);
668 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
670 S_netboot_info_p
= netboot_info_init(iaddr
);
671 switch (S_netboot_info_p
->image_type
) {
673 case kNetBootImageTypeNFS
:
674 error
= nfs_mountroot();
676 case kNetBootImageTypeHTTP
:
677 error
= netboot_setup(procp
);
691 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
696 netboot_setup(struct proc
* p
)
701 if (S_netboot_info_p
== NULL
702 || S_netboot_info_p
->image_path
== NULL
) {
705 if (S_netboot_info_p
->use_hdix
) {
706 printf("netboot_setup: calling di_root_image\n");
707 error
= di_root_image(S_netboot_info_p
->image_path
,
710 printf("netboot_setup: di_root_image: failed %d\n", error
);
715 printf("netboot_setup: calling vndevice_root_image\n");
716 error
= vndevice_root_image(S_netboot_info_p
->image_path
,
719 printf("netboot_setup: vndevice_root_image: failed %d\n", error
);
725 printf("netboot: root device 0x%x\n", rootdev
);
726 error
= vfs_mountroot();
727 if (error
== 0 && rootvnode
!= NULL
) {
731 /* Get the vnode for '/'. Set fdp->fd_fd.fd_cdir to reference it. */
732 if (VFS_ROOT(mountlist
.cqh_last
, &newdp
))
733 panic("netboot_setup: cannot find root vnode");
737 filedesc0
.fd_cdir
= newdp
;
739 simple_lock(&mountlist_slock
);
740 CIRCLEQ_REMOVE(&mountlist
, CIRCLEQ_FIRST(&mountlist
), mnt_list
);
741 simple_unlock(&mountlist_slock
);
742 VOP_UNLOCK(rootvnode
, 0, p
);
743 mountlist
.cqh_first
->mnt_flag
|= MNT_ROOTFS
;
746 netboot_info_free(&S_netboot_info_p
);