• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* ----------------------------------------------------------------------- *
2  *
3  *   Copyright 2007-2009 H. Peter Anvin - All Rights Reserved
4  *   Copyright 2009 Intel Corporation; author: H. Peter Anvin
5  *
6  *   Permission is hereby granted, free of charge, to any person
7  *   obtaining a copy of this software and associated documentation
8  *   files (the "Software"), to deal in the Software without
9  *   restriction, including without limitation the rights to use,
10  *   copy, modify, merge, publish, distribute, sublicense, and/or
11  *   sell copies of the Software, and to permit persons to whom
12  *   the Software is furnished to do so, subject to the following
13  *   conditions:
14  *
15  *   The above copyright notice and this permission notice shall
16  *   be included in all copies or substantial portions of the Software.
17  *
18  *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19  *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20  *   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21  *   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22  *   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23  *   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24  *   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25  *   OTHER DEALINGS IN THE SOFTWARE.
26  *
27  * ----------------------------------------------------------------------- */
28 
29 /*
30  * shuffle_pm.c
31  *
32  * Shuffle and boot to protected mode code
33  */
34 
35 #include <inttypes.h>
36 #include <syslinux/movebits.h>
37 #include <syslinux/bootpm.h>
38 
syslinux_shuffle_boot_pm(struct syslinux_movelist * fraglist,struct syslinux_memmap * memmap,uint16_t bootflags,struct syslinux_pm_regs * regs)39 int syslinux_shuffle_boot_pm(struct syslinux_movelist *fraglist,
40 			     struct syslinux_memmap *memmap,
41 			     uint16_t bootflags, struct syslinux_pm_regs *regs)
42 {
43     uint8_t handoff_code[9 * 5], *p;
44     const uint32_t *rp;
45     int i, rv;
46     struct syslinux_memmap *tmap;
47     addr_t regstub, stublen;
48 
49     tmap = syslinux_target_memmap(fraglist, memmap);
50     if (!tmap)
51 	return -1;
52 
53     regstub = 0x800;		/* Locate anywhere above this point */
54     stublen = sizeof handoff_code;
55     rv = syslinux_memmap_find_type(tmap, SMT_FREE, &regstub, &stublen, 1);
56     syslinux_free_memmap(tmap);
57     if (rv)
58 	return -1;
59 
60     /* Build register-setting stub */
61     p = handoff_code;
62     rp = (const uint32_t *)regs;
63     for (i = 0; i < 8; i++) {
64 	*p = 0xb8 + i;		/* MOV gpr,imm32 */
65 	*(uint32_t *) (p + 1) = *rp++;
66 	p += 5;
67     }
68     *p = 0xe9;			/* JMP */
69     *(uint32_t *) (p + 1) = regs->eip - regstub - sizeof handoff_code;
70 
71     /* Add register-setting stub to shuffle list */
72     if (syslinux_add_movelist(&fraglist, regstub, (addr_t) handoff_code,
73 			      sizeof handoff_code))
74 	return -1;
75 
76     return syslinux_do_shuffle(fraglist, memmap, regstub, 1, bootflags);
77 }
78