Imported Debian patch 3.4-9
[debian/elilo] / bootparams.c
1 /*
2  *  Copyright (C) 2001-2003 Hewlett-Packard Co.
3  *      Contributed by Stephane Eranian <eranian@hpl.hp.com>
4  *
5  * This file is part of the ELILO, the EFI Linux boot loader.
6  *
7  *  ELILO is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2, or (at your option)
10  *  any later version.
11  *
12  *  ELILO is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with ELILO; see the file COPYING.  If not, write to the Free
19  *  Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20  *  02111-1307, USA.
21  *
22  * Please check out the elilo.txt for complete documentation on how
23  * to use this program.
24  */
25
26 #include <efi.h>
27 #include <efilib.h>
28
29 #include "elilo.h"
30
31 /*
32  * Initialize the generic part of the boot parameters and call the architecture specific
33  * subroutine.
34  * Output:
35  *      cookie: the memory map cookie to use with ExitBootServices()
36  * Return:
37  *      NULL: if an error occured
38  *      bp  : the address of the bootparams otherwise (opaque type)
39  */
40 VOID *
41 create_boot_params(CHAR16 *args, memdesc_t *initrd, UINTN *cookie)
42 {
43 /* 
44  * XXX: need cleanup
45  * See ia32 code for explanation on why it is so big.
46  */
47 #define BOOT_PARAM_MEMSIZE      16384   
48         UINTN bpsize, cmdline_size;
49         boot_params_t *bp;
50         CHAR8 *cp;
51         CHAR16 ch;
52
53         /*
54          * Allocate runtime services memory to hold memory descriptor table and
55          * command line arguments and fetch memory map:
56          *
57          * arg_size = number of character in ASCII form
58          */
59         cmdline_size = StrLen(args) + 1;
60         bpsize       = sizeof(boot_params_t) + cmdline_size;
61
62         if (bpsize > BOOT_PARAM_MEMSIZE) {
63                 ERR_PRT((L"BOOT_PARAM_MEMSIZE too small, need at least %d bytes", bpsize));
64                 return NULL;
65         }
66
67
68         /*
69          * Allocate memory for boot parameters.
70          * This CANNOT be EfiLoaderData or EfiLoaderCode as the kernel
71          * frees this region when initializing.
72          */
73
74         bp = (boot_params_t *)alloc(BOOT_PARAM_MEMSIZE, EfiLoaderData);
75         if (bp == NULL) {
76                 ERR_PRT((L"can't allocate boot params"));
77                 return 0;
78         }
79
80         VERB_PRT(3, Print(L"boot params @ 0x%lx\n", bp));
81
82 /* XXX: need to fix this for 3.5 */
83 #ifdef CONFIG_ia64
84        cp = ((CHAR8 *)bp) + BOOT_PARAM_MEMSIZE - cmdline_size;
85 #elif defined CONFIG_ia32
86        cp = ((CHAR8 *)bp) + BOOT_PARAM_MEMSIZE - 2048;
87 #endif
88
89         /* 
90          * clear entire buffer. The boot param structure is bigger than
91          * needs be but this allows the kernel bootparam structure to grow
92          * (up to BOOT_PARAM_MEMSIZE) without having to worry about fixing the bootloader.
93          * By convention between the laoder and the kernel, the value 0 means 
94          * don't care or not set.
95          */
96         Memset(bp, 0, BOOT_PARAM_MEMSIZE);
97
98         if (sysdeps_create_boot_params(bp, cp, initrd, cookie) == -1) return 0;
99
100         /*
101          * Convert kernel command line args from UNICODE to ASCII and put them where
102          * the kernel expects them:
103          */
104         while (1) {
105                 ch = *args++;
106                 if (!ch) break;
107                 *cp++ = ch;
108         }
109         *cp++ = '\0';
110
111         return bp;
112 }
113
114 VOID
115 free_boot_params(VOID *bp)
116 {
117         boot_params_t *real_bp = (boot_params_t *)bp;
118
119         sysdeps_free_boot_params(real_bp);
120
121         free(real_bp);
122 }
123