1 /*
2  *  Copyright (C) 2005 IBM Corporation
3  *
4  *  Authors:
5  *  Reiner Sailer, <sailer@watson.ibm.com>
6  *  Stefan Berger, <stefanb@watson.ibm.com>
7  *
8  *  Contributors:
9  *  Michael LeMay, <mdlemay@epoch.ncsc.mil>
10  *  George Coker, <gscoker@alpha.ncsc.mil>
11  *
12  *  This program is free software; you can redistribute it and/or modify
13  *  it under the terms of the GNU General Public License version 2,
14  *  as published by the Free Software Foundation.
15  *
16  *
17  *  This file contains the XSM policy init functions for Xen.
18  *
19  */
20 
21 #include <xsm/xsm.h>
22 #ifdef CONFIG_MULTIBOOT
23 #include <xen/multiboot.h>
24 #endif
25 #include <xen/bitops.h>
26 #ifdef CONFIG_HAS_DEVICE_TREE
27 # include <asm/setup.h>
28 # include <xen/device_tree.h>
29 #endif
30 
31 #ifdef CONFIG_MULTIBOOT
xsm_multiboot_policy_init(unsigned long * module_map,const multiboot_info_t * mbi,void * (* bootstrap_map)(const module_t *),void ** policy_buffer,size_t * policy_size)32 int __init xsm_multiboot_policy_init(unsigned long *module_map,
33                                      const multiboot_info_t *mbi,
34                                      void *(*bootstrap_map)(const module_t *),
35                                      void **policy_buffer,
36                                      size_t *policy_size)
37 {
38     int i;
39     module_t *mod = (module_t *)__va(mbi->mods_addr);
40     int rc = 0;
41     u32 *_policy_start;
42     unsigned long _policy_len;
43 
44     /*
45      * Try all modules and see whichever could be the binary policy.
46      * Adjust module_map for the module that is the binary policy.
47      */
48     for ( i = mbi->mods_count-1; i >= 1; i-- )
49     {
50         if ( !test_bit(i, module_map) )
51             continue;
52 
53         _policy_start = bootstrap_map(mod + i);
54         _policy_len   = mod[i].mod_end;
55 
56         if ( (xsm_magic_t)(*_policy_start) == XSM_MAGIC )
57         {
58             *policy_buffer = _policy_start;
59             *policy_size = _policy_len;
60 
61             printk("Policy len %#lx, start at %p.\n",
62                    _policy_len,_policy_start);
63 
64             __clear_bit(i, module_map);
65             break;
66 
67         }
68 
69         bootstrap_map(NULL);
70     }
71 
72     return rc;
73 }
74 #endif
75 
76 #ifdef CONFIG_HAS_DEVICE_TREE
xsm_dt_policy_init(void ** policy_buffer,size_t * policy_size)77 int __init xsm_dt_policy_init(void **policy_buffer, size_t *policy_size)
78 {
79     struct bootmodule *mod = boot_module_find_by_kind(BOOTMOD_XSM);
80     paddr_t paddr, len;
81 
82     if ( !mod || !mod->size )
83         return 0;
84 
85     paddr = mod->start;
86     len = mod->size;
87 
88     if ( !has_xsm_magic(paddr) )
89     {
90         printk(XENLOG_ERR "xsm: Invalid magic for XSM blob\n");
91         return -EINVAL;
92     }
93 
94     printk("xsm: Policy len = 0x%"PRIpaddr" start at 0x%"PRIpaddr"\n",
95            len, paddr);
96 
97     *policy_buffer = xmalloc_bytes(len);
98     if ( !*policy_buffer )
99         return -ENOMEM;
100 
101     copy_from_paddr(*policy_buffer, paddr, len);
102     *policy_size = len;
103 
104     return 0;
105 }
106 #endif
107 
108 /*
109  * Local variables:
110  * mode: C
111  * c-file-style: "BSD"
112  * c-basic-offset: 4
113  * tab-width: 4
114  * indent-tabs-mode: nil
115  * End:
116  */
117