Justin L Werner | dc80e00 | 2012-08-09 14:20:30 -0600 | [diff] [blame^] | 1 | /* |
| 2 | * magic.c |
| 3 | * Copyright (c) 2012 Canonical LTD. |
| 4 | * Author: Justin L Werner <justin.werner@caononical.com> |
| 5 | * See: RFC 5071 |
| 6 | * |
| 7 | * See file CREDITS for list of people who contributed to this |
| 8 | * project. |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or |
| 11 | * modify it under the terms of the GNU General Public License as |
| 12 | * published by the Free Software Foundation; either version 2 of |
| 13 | * the License, or (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program; if not, write to the Free Software |
| 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 23 | * MA 02111-1307 USA |
| 24 | * |
| 25 | */ |
| 26 | |
| 27 | #include <common.h> |
| 28 | #include <command.h> |
| 29 | #include <net.h> |
| 30 | #include "bootp.h" |
| 31 | |
| 32 | #include <linux/compiler.h> |
| 33 | |
| 34 | #if defined(CONFIG_BOOTP_VENDOREX) && defined(CONFIG_BOOTP_VENDOREX_PXE_SHARED) |
| 35 | #include "magic.h" |
| 36 | |
| 37 | #define OPT_MAGIC 208 |
| 38 | #define OPT_CONFIG_FILE 209 |
| 39 | #define OPT_PATH_PREFIX 210 |
| 40 | #define OPT_REBOOT_TIME 211 |
| 41 | |
| 42 | #define OPT_MAGIC_LEN 4 |
| 43 | #define OPT_REBOOT_TIME_LEN 4 |
| 44 | |
| 45 | |
| 46 | static const u8 opt_magic_string[] = {0xf1, 0x0, 0x74, 0x7e}; |
| 47 | |
| 48 | static int opt_magic_seen = 0; |
| 49 | static u8 opt_config_file_value[UCHAR_MAX + 1]; |
| 50 | static u8 opt_path_prefix_value[UCHAR_MAX + 1]; |
| 51 | static size_t opt_reboot_time = 0; |
| 52 | |
| 53 | static u8 *parse_magic(u8 *); |
| 54 | static u8 *parse_config_file(u8 *); |
| 55 | static u8 *parse_path_prefix(u8 *); |
| 56 | static u8 *parse_reboot_time(u8 *); |
| 57 | |
| 58 | |
| 59 | /* |
| 60 | * Init/done processing any vendor extension options that we care |
| 61 | * about here. It is possible (at least in my env) for the PXE options |
| 62 | * processing to occur more than once. |
| 63 | */ |
| 64 | void |
| 65 | dhcp_vendorex_opts_done(void) |
| 66 | { |
| 67 | opt_magic_seen = 0; |
| 68 | memset(opt_config_file_value, 0, sizeof(opt_config_file_value)); |
| 69 | memset(opt_path_prefix_value, 0, sizeof(opt_path_prefix_value)); |
| 70 | opt_reboot_time = 0; |
| 71 | } |
| 72 | |
| 73 | /* |
| 74 | * Add vendor option(s) to the request |
| 75 | * - add own opts and return e as ptr just beyond them |
| 76 | * |
| 77 | * For our common PXE MAGIC implementation, we don't add anything: it's a noop |
| 78 | */ |
| 79 | u8 * |
| 80 | dhcp_vendorex_prep (u8 *e) |
| 81 | { |
| 82 | return e; |
| 83 | } |
| 84 | |
| 85 | /* |
| 86 | * Parse vendor options from a response |
| 87 | * - e points into the bp_vend[] array, which is OPT_SIZE |
| 88 | * returns next 'e' if recognized and accepted, NULL otherwise |
| 89 | */ |
| 90 | u8 * |
| 91 | dhcp_vendorex_proc (u8 *e) |
| 92 | { |
| 93 | switch (*e) { |
| 94 | case OPT_MAGIC: |
| 95 | printf("INFO: DHCP PXE vendorex PXE MAGIC option encountered\n"); |
| 96 | e = parse_magic(++e); |
| 97 | break; |
| 98 | |
| 99 | case OPT_CONFIG_FILE: |
| 100 | printf("INFO: DHCP PXE vendorex PXE ConfigFile option encountered\n"); |
| 101 | e = parse_config_file(++e); |
| 102 | break; |
| 103 | |
| 104 | case OPT_PATH_PREFIX: |
| 105 | printf("INFO: DHCP PXE vendorex PXE PathPrefix option encountered\n"); |
| 106 | e = parse_path_prefix(++e); |
| 107 | break; |
| 108 | |
| 109 | case OPT_REBOOT_TIME: |
| 110 | printf("INFO: DHCP PXE vendorex RebootTime option encountered\n"); |
| 111 | e = parse_reboot_time(++e); |
| 112 | break; |
| 113 | |
| 114 | default: |
| 115 | e = NULL; |
| 116 | break; |
| 117 | } |
| 118 | |
| 119 | return e; |
| 120 | } |
| 121 | |
| 122 | static u8 * |
| 123 | parse_magic(u8 *e) |
| 124 | { |
| 125 | size_t len = *e++; |
| 126 | |
| 127 | if (opt_magic_seen) { |
| 128 | printf("INFO: DHCP PXE vendorex MAGIC option already seen\n"); |
| 129 | } |
| 130 | |
| 131 | if (len != OPT_MAGIC_LEN) { |
| 132 | e = NULL; |
| 133 | printf("ERROR: DHCP PXE vendorex MAGIC has invalid length [%u]\n", len); |
| 134 | |
| 135 | } else if (memcmp(e, opt_magic_string, sizeof(opt_magic_string))) { |
| 136 | e = NULL; |
| 137 | opt_magic_seen = 0; |
| 138 | printf("ERROR: DHCP PXE vendorex MAGIC invalid magic value %02x:%02x:%02x:%02x\n", |
| 139 | (unsigned int) e[0], (unsigned int) e[1], (unsigned int) e[2], (unsigned int) e[3]); |
| 140 | |
| 141 | } else { |
| 142 | e += sizeof(opt_magic_string); |
| 143 | opt_magic_seen = 1; |
| 144 | printf("INFO: DHCP PXE vendorex MAGIC confirmed\n"); |
| 145 | } |
| 146 | |
| 147 | return e; |
| 148 | } |
| 149 | |
| 150 | static u8 * |
| 151 | parse_config_file(u8 *e) |
| 152 | { |
| 153 | size_t len = *e++; |
| 154 | |
| 155 | if (! opt_magic_seen) { |
| 156 | printf("ERROR: DHCP PXE vendorex ConfigFile without MAGIC\n"); |
| 157 | e = NULL; |
| 158 | |
| 159 | } else if (len == 0) { |
| 160 | printf("WARNING: DHCP PXE vendorex ConfigFile string has zero length, ignoring\n"); |
| 161 | |
| 162 | } else { |
| 163 | memset(opt_config_file_value, 0, sizeof(opt_config_file_value)); |
| 164 | memcpy(opt_config_file_value, e, len); |
| 165 | e += len; |
| 166 | opt_config_file_value[len] = 0; |
| 167 | setenv(PXE_CONFIG_FILE, (char *)opt_config_file_value); |
| 168 | printf("INFO: DHCP PXE vendorex ConfigFile is [%s]\n", opt_config_file_value); |
| 169 | } |
| 170 | |
| 171 | return e; |
| 172 | } |
| 173 | |
| 174 | static u8 * |
| 175 | parse_path_prefix(u8 *e) |
| 176 | { |
| 177 | size_t len = *e++; |
| 178 | |
| 179 | if (! opt_magic_seen) { |
| 180 | printf("ERROR: DHCP PXE vendorex ConfigFile without MAGIC\n"); |
| 181 | e = NULL; |
| 182 | |
| 183 | } if (len == 0) { |
| 184 | printf("WARNING: DHCP PXE vendorex PathPrefix string has zero length, ignoring\n"); |
| 185 | |
| 186 | } else { |
| 187 | memset(opt_path_prefix_value, 0, sizeof(opt_path_prefix_value)); |
| 188 | memcpy(opt_path_prefix_value, e, len); |
| 189 | e += len; |
| 190 | opt_path_prefix_value[len] = 0; |
| 191 | setenv(PXE_PATH_PREFIX, (char *)opt_path_prefix_value); |
| 192 | printf("INFO: DHCP PXE vendorex PathPrefix is [%s]\n", opt_path_prefix_value); |
| 193 | } |
| 194 | |
| 195 | return e; |
| 196 | } |
| 197 | |
| 198 | static u8 * |
| 199 | parse_reboot_time(u8 *e) |
| 200 | { |
| 201 | size_t len = *e++; |
| 202 | char bfr[12]; |
| 203 | |
| 204 | if (! opt_magic_seen) { |
| 205 | printf("ERROR: DHCP PXE vendorex ConfigFile without MAGIC\n"); |
| 206 | e = NULL; |
| 207 | |
| 208 | } else if (len != OPT_REBOOT_TIME_LEN) { |
| 209 | e += len; |
| 210 | printf("WARNING: DHCP PXE vendorex RebootTime has invalid length [%u], ignoring\n", len); |
| 211 | |
| 212 | } else { |
| 213 | unsigned long value; |
| 214 | |
| 215 | memcpy(&value, e, OPT_REBOOT_TIME_LEN); |
| 216 | opt_reboot_time = (size_t) ntohl(value); |
| 217 | e += OPT_REBOOT_TIME_LEN; |
| 218 | |
| 219 | sprintf(bfr, "%u", opt_reboot_time); |
| 220 | setenv(PXE_REBOOT_TIME, bfr); |
| 221 | |
| 222 | printf("INFO: DHCP PXE vendorex RebootTime is [%u]\n", opt_reboot_time); |
| 223 | |
| 224 | #if defined(CONFIG_BOOT_RETRY_TIME) |
| 225 | if (opt_reboot_time < CONFIG_BOOT_RETRY_MIN) { |
| 226 | opt_reboot_time = CONFIG_BOOT_RETRY_MIN; |
| 227 | sprintf(bfr, "%u", opt_reboot_time); |
| 228 | setenv(PXE_REBOOT_TIME, bfr); |
| 229 | printf("INFO: DHCP PXE vendorex RebootTime adjusted up to allowable minimum value [%d]\n", |
| 230 | CONFIG_BOOT_RETRY_MIN); |
| 231 | } |
| 232 | reset_cmd_timeout(); |
| 233 | #else |
| 234 | printf("INFO: DHCP PXE vendorex RebootTime is N/A and ignored due to u-boot configs\n"); |
| 235 | #endif |
| 236 | } |
| 237 | |
| 238 | return e; |
| 239 | } |
| 240 | #else |
| 241 | const int no_arm_vendorex_options = 1; |
| 242 | #endif |