From 0787fba15352ef47958e0c9fcc912b8bbcf8ab4e Mon Sep 17 00:00:00 2001 From: Paulo Flabiano Smorigo Date: Mon, 30 Jun 2014 10:31:59 -0300 Subject: [PATCH 1/2] Add bootargs parser for open firmware. It enables net boot even when there is no bootp/dhcp server. --- ChangeLog | 16 +++++ grub-core/kern/ieee1275/init.c | 7 +-- grub-core/net/bootp.c | 63 +++++-------------- grub-core/net/drivers/ieee1275/ofnet.c | 107 ++++++++++++++++++++++++++++++++- grub-core/net/net.c | 38 ++++++++++++ include/grub/ieee1275/ieee1275.h | 3 +- include/grub/net.h | 4 ++ 7 files changed, 185 insertions(+), 53 deletions(-) diff --git a/ChangeLog b/ChangeLog index 41bcebf..a659369 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,19 @@ +2014-01-24 Paulo Flabiano Smorigo + + Add bootargs parser for open firmware. + + It enables net boot even when there is no bootp/dhcp server. + + * grub-core/net/drivers/ieee1275/ofnet.c: Add grub_ieee1275_parse_bootargs + and call it at grub_ieee1275_net_config_real. + +2013-12-23 Paulo Flabiano Smorigo + + Add grub_env_set_net_property function. + + * grub-core/net/bootp.c: Remove set_env_limn_ro. + * grub-core/net/net.c: Add grub_env_set_net_property. + 2013-12-24 Vladimir Serbinenko * configure.ac: Set version to 2.02~beta2. diff --git a/grub-core/kern/ieee1275/init.c b/grub-core/kern/ieee1275/init.c index 89b2822..d5bd74d 100644 --- a/grub-core/kern/ieee1275/init.c +++ b/grub-core/kern/ieee1275/init.c @@ -80,9 +80,8 @@ grub_translate_ieee1275_path (char *filepath) } } -void (*grub_ieee1275_net_config) (const char *dev, - char **device, - char **path); +void (*grub_ieee1275_net_config) (const char *dev, char **device, char **path, + char *bootpath); void grub_machine_get_bootlocation (char **device, char **path) { @@ -126,7 +125,7 @@ grub_machine_get_bootlocation (char **device, char **path) *ptr = 0; if (grub_ieee1275_net_config) - grub_ieee1275_net_config (canon, device, path); + grub_ieee1275_net_config (canon, device, path, bootpath); grub_free (dev); grub_free (canon); } diff --git a/grub-core/net/bootp.c b/grub-core/net/bootp.c index c14e9de..6310ed4 100644 --- a/grub-core/net/bootp.c +++ b/grub-core/net/bootp.c @@ -25,41 +25,6 @@ #include #include -static char * -grub_env_write_readonly (struct grub_env_var *var __attribute__ ((unused)), - const char *val __attribute__ ((unused))) -{ - return NULL; -} - -static void -set_env_limn_ro (const char *intername, const char *suffix, - const char *value, grub_size_t len) -{ - char *varname, *varvalue; - char *ptr; - varname = grub_xasprintf ("net_%s_%s", intername, suffix); - if (!varname) - return; - for (ptr = varname; *ptr; ptr++) - if (*ptr == ':') - *ptr = '_'; - varvalue = grub_malloc (len + 1); - if (!varvalue) - { - grub_free (varname); - return; - } - - grub_memcpy (varvalue, value, len); - varvalue[len] = 0; - grub_env_set (varname, varvalue); - grub_register_variable_hook (varname, 0, grub_env_write_readonly); - grub_env_export (varname); - grub_free (varname); - grub_free (varvalue); -} - static void parse_dhcp_vendor (const char *name, const void *vend, int limit, int *mask) { @@ -136,20 +101,24 @@ parse_dhcp_vendor (const char *name, const void *vend, int limit, int *mask) } continue; case GRUB_NET_BOOTP_HOSTNAME: - set_env_limn_ro (name, "hostname", (const char *) ptr, taglength); - break; + grub_env_set_net_property (name, "hostname", (const char *) ptr, + taglength); + break; case GRUB_NET_BOOTP_DOMAIN: - set_env_limn_ro (name, "domain", (const char *) ptr, taglength); - break; + grub_env_set_net_property (name, "domain", (const char *) ptr, + taglength); + break; case GRUB_NET_BOOTP_ROOT_PATH: - set_env_limn_ro (name, "rootpath", (const char *) ptr, taglength); - break; + grub_env_set_net_property (name, "rootpath", (const char *) ptr, + taglength); + break; case GRUB_NET_BOOTP_EXTENSIONS_PATH: - set_env_limn_ro (name, "extensionspath", (const char *) ptr, taglength); - break; + grub_env_set_net_property (name, "extensionspath", (const char *) ptr, + taglength); + break; /* If you need any other options please contact GRUB development team. */ @@ -211,8 +180,8 @@ grub_net_configure_by_dhcp_ack (const char *name, } if (size > OFFSET_OF (boot_file, bp)) - set_env_limn_ro (name, "boot_file", (char *) bp->boot_file, - sizeof (bp->boot_file)); + grub_env_set_net_property (name, "boot_file", bp->boot_file, + sizeof (bp->boot_file)); if (is_def) grub_net_default_server = 0; if (is_def && !grub_net_default_server && bp->server_ip) @@ -243,8 +212,8 @@ grub_net_configure_by_dhcp_ack (const char *name, if (size > OFFSET_OF (server_name, bp) && bp->server_name[0]) { - set_env_limn_ro (name, "dhcp_server_name", (char *) bp->server_name, - sizeof (bp->server_name)); + grub_env_set_net_property (name, "dhcp_server_name", bp->server_name, + sizeof (bp->server_name)); if (is_def && !grub_net_default_server) { grub_net_default_server = grub_strdup (bp->server_name); diff --git a/grub-core/net/drivers/ieee1275/ofnet.c b/grub-core/net/drivers/ieee1275/ofnet.c index ceb5931..a079065 100644 --- a/grub-core/net/drivers/ieee1275/ofnet.c +++ b/grub-core/net/drivers/ieee1275/ofnet.c @@ -127,8 +127,111 @@ bootp_response_properties[] = { .name = "bootpreply-packet", .offset = 0x2a}, }; +enum +{ + BOOTARGS_SERVER_ADDR, + BOOTARGS_FILENAME, + BOOTARGS_CLIENT_ADDR, + BOOTARGS_GATEWAY_ADDR, + BOOTARGS_BOOTP_RETRIES, + BOOTARGS_TFTP_RETRIES, + BOOTARGS_SUBNET_MASK, + BOOTARGS_BLOCKSIZE +}; + +static int +grub_ieee1275_parse_bootargs (const char *devpath, char *bootpath, + char **device, struct grub_net_card **card) +{ + char *args; + char *comma_char = 0; + char *equal_char = 0; + grub_size_t field_counter = 0; + + grub_net_network_level_address_t client_addr, gateway_addr, subnet_mask; + grub_net_link_level_address_t hw_addr; + grub_net_interface_flags_t flags = 0; + struct grub_net_network_level_interface *inter; + + hw_addr.type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET; + + args = bootpath + grub_strlen (devpath) + 1; + do + { + comma_char = grub_strchr (args, ','); + if (comma_char != 0) + *comma_char = 0; + + /* Check if it's an option (like speed=auto) and not a default parameter */ + equal_char = grub_strchr (args, '='); + if (equal_char != 0) + { + *equal_char = 0; + grub_env_set_net_property ((*card)->name, args, equal_char + 1, + grub_strlen(equal_char + 1)); + *equal_char = '='; + } + else + { + switch (field_counter++) + { + case BOOTARGS_SERVER_ADDR: + *device = grub_xasprintf ("tftp,%s", args); + if (!*device) + return grub_errno; + break; + + case BOOTARGS_CLIENT_ADDR: + grub_net_resolve_address (args, &client_addr); + break; + + case BOOTARGS_GATEWAY_ADDR: + grub_net_resolve_address (args, &gateway_addr); + break; + + case BOOTARGS_SUBNET_MASK: + grub_net_resolve_address (args, &subnet_mask); + break; + } + } + args = comma_char + 1; + if (comma_char != 0) + *comma_char = ','; + } while (comma_char != 0); + + if ((client_addr.ipv4 != 0) && (subnet_mask.ipv4 != 0)) + { + grub_ieee1275_phandle_t devhandle; + grub_ieee1275_finddevice (devpath, &devhandle); + grub_ieee1275_get_property (devhandle, "mac-address", + hw_addr.mac, sizeof(hw_addr.mac), 0); + inter = grub_net_add_addr ((*card)->name, *card, &client_addr, &hw_addr, + flags); + grub_net_add_ipv4_local (inter, + __builtin_ctz (~grub_le_to_cpu32 (subnet_mask.ipv4))); + } + + if (gateway_addr.ipv4 != 0) + { + grub_net_network_level_netaddress_t target; + char *rname; + + target.type = GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV4; + target.ipv4.base = 0; + target.ipv4.masksize = 0; + rname = grub_xasprintf ("%s:default", ((*card)->name)); + if (rname) + grub_net_add_route_gw (rname, target, gateway_addr); + else + return grub_errno; + } + + return 0; +} + static void -grub_ieee1275_net_config_real (const char *devpath, char **device, char **path) +grub_ieee1275_net_config_real (const char *devpath, char **device, char **path, + char *bootpath) { struct grub_net_card *card; @@ -158,6 +261,8 @@ grub_ieee1275_net_config_real (const char *devpath, char **device, char **path) } grub_free (canon); + grub_ieee1275_parse_bootargs (devpath, bootpath, device, &card); + for (i = 0; i < ARRAY_SIZE (bootp_response_properties); i++) if (grub_ieee1275_get_property_length (grub_ieee1275_chosen, bootp_response_properties[i].name, diff --git a/grub-core/net/net.c b/grub-core/net/net.c index 0e57e93..8f9d183 100644 --- a/grub-core/net/net.c +++ b/grub-core/net/net.c @@ -1480,6 +1480,44 @@ receive_packets (struct grub_net_card *card, int *stop_condition) grub_print_error (); } +static char * +grub_env_write_readonly (struct grub_env_var *var __attribute__ ((unused)), + const char *val __attribute__ ((unused))) +{ + return NULL; +} + +grub_err_t +grub_env_set_net_property (const char *intername, const char *suffix, + const char *value, grub_size_t len) +{ + char *varname, *varvalue; + char *ptr; + + varname = grub_xasprintf ("net_%s_%s", intername, suffix); + if (!varname) + return grub_errno; + for (ptr = varname; *ptr; ptr++) + if (*ptr == ':') + *ptr = '_'; + varvalue = grub_malloc (len + 1); + if (!varvalue) + { + grub_free (varname); + return grub_errno; + } + + grub_memcpy (varvalue, value, len); + varvalue[len] = 0; + grub_err_t ret = grub_env_set (varname, varvalue); + grub_register_variable_hook (varname, 0, grub_env_write_readonly); + grub_env_export (varname); + grub_free (varname); + grub_free (varvalue); + + return ret; +} + void grub_net_poll_cards (unsigned time, int *stop_condition) { diff --git a/include/grub/ieee1275/ieee1275.h b/include/grub/ieee1275/ieee1275.h index 7fefd16..f8b3d79 100644 --- a/include/grub/ieee1275/ieee1275.h +++ b/include/grub/ieee1275/ieee1275.h @@ -73,7 +73,8 @@ struct grub_ieee1275_devalias extern void (*EXPORT_VAR(grub_ieee1275_net_config)) (const char *dev, char **device, - char **path); + char **path, + char *bootargs); /* Maps a device alias to a pathname. */ extern grub_ieee1275_phandle_t EXPORT_VAR(grub_ieee1275_chosen); diff --git a/include/grub/net.h b/include/grub/net.h index de6259e..843f74f 100644 --- a/include/grub/net.h +++ b/include/grub/net.h @@ -480,6 +480,10 @@ grub_net_addr_to_str (const grub_net_network_level_address_t *target, void grub_net_hwaddr_to_str (const grub_net_link_level_address_t *addr, char *str); +grub_err_t +grub_env_set_net_property (const char *intername, const char *suffix, + const char *value, grub_size_t len); + void grub_net_poll_cards (unsigned time, int *stop_condition); -- 1.9.3