Branch
Hash :
1a08f436
Author :
Date :
2010-08-25T09:23:17
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 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
\ ========== Copyright Header Begin ==========================================
\
\ Hypervisor Software File: args.fth
\
\ Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
\
\ - Do no alter or remove copyright notices
\
\ - Redistribution and use of this software in source and binary forms, with
\ or without modification, are permitted provided that the following
\ conditions are met:
\
\ - Redistribution of source code must retain the above copyright notice,
\ this list of conditions and the following disclaimer.
\
\ - Redistribution in binary form must reproduce the above copyright notice,
\ this list of conditions and the following disclaimer in the
\ documentation and/or other materials provided with the distribution.
\
\ Neither the name of Sun Microsystems, Inc. or the names of contributors
\ may be used to endorse or promote products derived from this software
\ without specific prior written permission.
\
\ This software is provided "AS IS," without a warranty of any kind.
\ ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
\ INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
\ PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN
\ MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR
\ ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
\ DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
\ OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
\ FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
\ DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
\ ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF
\ SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
\
\ You acknowledge that this software is not designed, licensed or
\ intended for use in the design, construction, operation or maintenance of
\ any nuclear facility.
\
\ ========== Copyright Header End ============================================
id: @(#)args.fth 1.1 04/09/07
purpose: Network boot support package argument processing
copyright: Copyright 2004 Sun Microsystems, Inc. All Rights Reserved
copyright: Use is subject to license terms.
headerless
\ Get next comma delimited argument. Used for old style argument processing.
: arg-nextfield ( $ -- rem$ field$ ) ascii , left-parse-string ;
\ Get next argument. Arguments are separated by commas and may consist
\ of a single key or a key=value pair. Commas may appear in the value
\ field if the value is a quoted string.
: arg-nextparam ( args$ -- rem$ value$ key$ )
" ," string-skipchars dup 0= if ( args$' )
null$ null$ exit
then ( args$' )
2dup 0 -rot bounds ?do ( args$' 0 )
i c@ dup ascii , = swap ascii = = or if
drop i c@ leave
then
loop ( args$' delim )
dup >r left-parse-string 2swap r> ( key$ $ delim )
ascii = <> over 0= or if ( key$ $ )
null$ 2rot exit ( rem$ value$ key$ )
then ( key$ $ )
over c@ ascii " = if ( key$ $ )
2dup 1 /string ascii " left-parse-string ( key$ $ $' val$ )
2swap dup if ( key$ $ val$ $' )
over c@ ascii , = ( key$ $ val$ $' ok? )
else ( key$ $ val$ $' )
2over ca+ c@ ascii " = ( key$ $ val$ $' ok? )
then nip nip ( key$ $ val$ ok? )
0= if ( key$ $ val$ )
." Bad quoted string '" 2swap type ." '" cr -1 throw
then ( key$ $ val$ )
nip 2+ string-split ( key$ rem$ value$ )
else ( key$ $ )
ascii , left-parse-string ( key$ rem$ value$ )
then ( key$ rem$ value$ )
2rot ( rem$ value$ key$ )
;
: set-inet-addr ( ip$ adr -- ) inet-aton 0= throw ;
: get-dnumber ( $ -- n ) $dnumber throw ;
: set-hostip ( ip$ -- ) my-ip-addr set-inet-addr ;
: set-subnet-mask ( ip$ -- ) my-netmask set-inet-addr ;
: set-router ( ip$ -- ) router-ip set-inet-addr ;
: set-tftp-server ( ip$ -- ) tftp-server-ip set-inet-addr ;
: set-hostname ( name$ -- ) hostname pack drop ;
\ Client identifiers may be specified either as the ASCII hexadecimal
\ representation of the identifier, or as a quoted string. The identifier
\ specified here is used, without any transformations, as the client
\ identifier in DHCP transactions and in the WANboot HTTP request
\ query string.
: (set-client-id) ( $ -- invalid? )
over c@ ascii " = if ( $ )
qdstring>string dup 2 MAX_CID_LEN between if ( cid$ )
client-id pack drop false ( false )
else ( $ )
2drop true ( true )
then ( invalid? )
else ( $ )
dup 2 mod 0= over 2/ 2 MAX_CID_LEN between and if ( $ )
hexascii-to-octet dup if ( cid,len )
client-id pack drop false ( false )
else ( $ )
2drop true ( true )
then ( invalid? )
else ( $ )
2drop true ( true )
then ( invalid? )
then ( invalid? )
;
: set-client-id ( $ -- ) (set-client-id) throw ;
\ Boot file URIs must be "safe-encoded". URIs containing commas are
\ presented as quoted strings. Replace all occurences of "\" or "|"
\ with "/".
: set-boot-file ( $ -- )
qdstring>string bootfile pack count 2dup bounds ?do
i c@ dup ascii \ = swap ascii | = or if ascii / i c! then
loop
2dup is-uri? if check-uri$-form else 2drop then
;
\ HTTP proxy server specification.
: set-http-proxy ( proxy$ -- )
2dup check-htproxy$-form http-proxy pack drop
;
: set-dhcp-retries ( $ -- ) get-dnumber to dhcp-max-retries ;
: set-tftp-retries ( $ -- ) get-dnumber to tftp-max-retries ;
: arg=protocol? ( $ -- flag )
2dup " rarp" $= >r 2dup " bootp" $= >r " dhcp" $= r> r> or or
;
\ Process protocol argument. "bootp" is treated as a synonym of "dhcp".
: process-protocol-arg ( $ -- )
2dup " bootp" $= if 2drop " dhcp" then ( strategy$ )
config-strategy pack drop ( )
;
\ Key table for handling arguments specifying configuration parameters
create cfgparam-args
" file" true ['] set-boot-file token-handler,
"host-ip" true ['] set-hostip token-handler,
"router-ip" true ['] set-router token-handler,
"subnet-mask" true ['] set-subnet-mask token-handler,
"client-id" true ['] set-client-id token-handler,
"hostname" true ['] set-hostname token-handler,
"http-proxy" true ['] set-http-proxy token-handler,
"dhcp-retries" true ['] set-dhcp-retries token-handler,
"tftp-retries" true ['] set-tftp-retries token-handler,
null$ 0 0 token-handler,
: (process-argument) ( value$ key$ xt -- )
>r 2swap r> catch if
2drop ." Improperly formatted value for '" type ." '" cr -1 throw
else
2drop
then
;
: process-argument ( value$ key$ -- )
2dup cfgparam-args find-token-handler 0= if ( value$ key$ )
." Unknown key '" type ." '" cr 2drop exit ( )
then ( value$ key$ xt )
3 pick 0= if ( value$ key$ xt )
drop ." Missing value for '" type ." '" cr -1 throw
then ( value$ key$ xt )
(process-argument) ( )
;
\ Process arguments specified in the new-style syntax.
\ [protocol,] [key=value,]*
\
\ When the key=value style syntax is used, absence of the protocol
\ parameter implies manual configuration.
: newstyle-args? ( args$ -- flag ) ascii = strchr 0<> ;
: process-newstyle-args ( args$ -- )
" manual" config-strategy pack drop ( args$ )
begin dup while ( args$ )
arg-nextparam 2dup arg=protocol? if ( rem$ null$ protocol$ )
2swap 2drop process-protocol-arg ( rem$ )
else ( rem$ value$ key$ )
process-argument ( rem$ )
then ( rem$ )
repeat 2drop ( )
;
\ Process arguments specified in (old-style) positional parameter syntax.
\ [dhcp|bootp|rarp,][server-ip],[filename],[client-ip],[router-ip],
\ [boot-retries],[tftp-retries],[subnet-mask]
: ?arg-nextfield ( $ -- rem$ field$ true | rem$ false )
arg-nextfield dup if true else 2drop false then
;
: process-oldstyle-args ( args$ -- )
2dup arg-nextfield 2dup arg=protocol? if ( args$ $ protocol$ )
process-protocol-arg 2swap 2drop ( $ )
else ( args$ $ arg$ )
2drop 2drop ( rem$ )
then ( rem$ )
?arg-nextfield if
"tftp-server" ['] set-tftp-server (process-argument)
then
?arg-nextfield if " file" process-argument then
?arg-nextfield if "host-ip" process-argument then
?arg-nextfield if "router-ip" process-argument then
?arg-nextfield if "dhcp-retries" process-argument then
?arg-nextfield if "tftp-retries" process-argument then
?arg-nextfield if "subnet-mask" process-argument then
2drop
;
\ Manual configuration requires that the client be provided with (at the
\ minimum) its own IP address, address of the boot server and the name
\ of the bootfile. Hence, the argument must be in URI form. URI syntax
\ would have been validated already - the code here verifies that the
\ file component in the URI has been specified.
: (check-manual-config-args) ( -- ok? )
my-ip-addr inaddr-any? if ( )
false exit ( false )
then ( )
bootfile count dup if ( $ )
2dup is-uri? if ( $ )
parse-uri 2drop 2drop nip 0<> ( ok? )
else ( $ )
2drop false ( false )
then ( ok? )
else ( null$ )
2drop false ( false )
then ( ok? )
;
: check-manual-config-args ( -- )
['] (check-manual-config-args) catch if
." Manual Configuration: "
." Host IP, boot server and filename must be specified" cr
-1 throw
then
;
\ Process package arguments specified either on the command line or in
\ 'network-boot-arguments'. If any argument is specified on the command
\ line, all arguments in 'network-boot-arguments' are ignored.
: process-args ( $ -- )
dup 0= if ( null$ )
2drop " network-boot-arguments" get-option-string ( args$ )
then ( args$ )
dup 0= if 2drop exit then ( args$ )
2dup newstyle-args? if ( args$ )
process-newstyle-args ( )
config-strategy count " manual" $= if ( )
check-manual-config-args ( )
then ( )
else ( args$ )
process-oldstyle-args ( )
then ( )
;
headers