~ubuntu-branches/debian/stretch/grub2/stretch

« back to all changes in this revision

Viewing changes to loader/i386/efi/xnu.c

Tags: upstream-1.98+20100705
ImportĀ upstreamĀ versionĀ 1.98+20100705

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 *  GRUB  --  GRand Unified Bootloader
3
 
 *  Copyright (C) 2009  Free Software Foundation, Inc.
4
 
 *
5
 
 *  GRUB is free software: you can redistribute it and/or modify
6
 
 *  it under the terms of the GNU General Public License as published by
7
 
 *  the Free Software Foundation, either version 3 of the License, or
8
 
 *  (at your option) any later version.
9
 
 *
10
 
 *  GRUB is distributed in the hope that it will be useful,
11
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
 *  GNU General Public License for more details.
14
 
 *
15
 
 *  You should have received a copy of the GNU General Public License
16
 
 *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
17
 
 */
18
 
 
19
 
#include <grub/env.h>
20
 
#include <grub/xnu.h>
21
 
#include <grub/cpu/xnu.h>
22
 
#include <grub/efi/api.h>
23
 
#include <grub/efi/efi.h>
24
 
#include <grub/efi/uga_draw.h>
25
 
#include <grub/pci.h>
26
 
#include <grub/misc.h>
27
 
 
28
 
/* Setup video for xnu. Big parts are copied from linux.c. */
29
 
 
30
 
static grub_efi_guid_t uga_draw_guid = GRUB_EFI_UGA_DRAW_GUID;
31
 
 
32
 
#define RGB_MASK        0xffffff
33
 
#define RGB_MAGIC       0x121314
34
 
#define LINE_MIN        800
35
 
#define LINE_MAX        4096
36
 
#define FBTEST_STEP     (0x10000 >> 2)
37
 
#define FBTEST_COUNT    8
38
 
 
39
 
static int
40
 
find_line_len (grub_uint32_t *fb_base, grub_uint32_t *line_len)
41
 
{
42
 
  grub_uint32_t *base = (grub_uint32_t *) (grub_target_addr_t) *fb_base;
43
 
  int i;
44
 
 
45
 
  for (i = 0; i < FBTEST_COUNT; i++, base += FBTEST_STEP)
46
 
    {
47
 
      if ((*base & RGB_MASK) == RGB_MAGIC)
48
 
        {
49
 
          int j;
50
 
 
51
 
          for (j = LINE_MIN; j <= LINE_MAX; j++)
52
 
            {
53
 
              if ((base[j] & RGB_MASK) == RGB_MAGIC)
54
 
                {
55
 
                  *fb_base = (grub_uint32_t) (grub_target_addr_t) base;
56
 
                  *line_len = j << 2;
57
 
 
58
 
                  return 1;
59
 
                }
60
 
            }
61
 
 
62
 
          break;
63
 
        }
64
 
    }
65
 
 
66
 
  return 0;
67
 
}
68
 
 
69
 
static int
70
 
find_framebuf (grub_uint32_t *fb_base, grub_uint32_t *line_len)
71
 
{
72
 
  int found = 0;
73
 
 
74
 
  auto int NESTED_FUNC_ATTR find_card (grub_pci_device_t dev,
75
 
                                       grub_pci_id_t pciid);
76
 
 
77
 
  int NESTED_FUNC_ATTR find_card (grub_pci_device_t dev,
78
 
                                  grub_pci_id_t pciid)
79
 
    {
80
 
      grub_pci_address_t addr;
81
 
 
82
 
      addr = grub_pci_make_address (dev, GRUB_PCI_REG_CLASS);
83
 
      if (grub_pci_read (addr) >> 24 == 0x3)
84
 
        {
85
 
          int i;
86
 
 
87
 
          grub_printf ("Display controller: %d:%d.%d\nDevice id: %x\n",
88
 
                       grub_pci_get_bus (dev), grub_pci_get_device (dev),
89
 
                       grub_pci_get_function (dev), pciid);
90
 
          addr += 8;
91
 
          for (i = 0; i < 6; i++, addr += 4)
92
 
            {
93
 
              grub_uint32_t old_bar1, old_bar2, type;
94
 
              grub_uint64_t base64;
95
 
 
96
 
              old_bar1 = grub_pci_read (addr);
97
 
              if ((! old_bar1) || (old_bar1 & GRUB_PCI_ADDR_SPACE_IO))
98
 
                continue;
99
 
 
100
 
              type = old_bar1 & GRUB_PCI_ADDR_MEM_TYPE_MASK;
101
 
              if (type == GRUB_PCI_ADDR_MEM_TYPE_64)
102
 
                {
103
 
                  if (i == 5)
104
 
                    break;
105
 
 
106
 
                  old_bar2 = grub_pci_read (addr + 4);
107
 
                }
108
 
              else
109
 
                old_bar2 = 0;
110
 
 
111
 
              base64 = old_bar2;
112
 
              base64 <<= 32;
113
 
              base64 |= (old_bar1 & GRUB_PCI_ADDR_MEM_MASK);
114
 
 
115
 
              grub_printf ("%s(%d): 0x%llx\n",
116
 
                           ((old_bar1 & GRUB_PCI_ADDR_MEM_PREFETCH) ?
117
 
                            "VMEM" : "MMIO"), i,
118
 
                           (unsigned long long) base64);
119
 
 
120
 
              if ((old_bar1 & GRUB_PCI_ADDR_MEM_PREFETCH) && (! found))
121
 
                {
122
 
                  *fb_base = base64;
123
 
                  if (find_line_len (fb_base, line_len))
124
 
                    found++;
125
 
                }
126
 
 
127
 
              if (type == GRUB_PCI_ADDR_MEM_TYPE_64)
128
 
                {
129
 
                  i++;
130
 
                  addr += 4;
131
 
                }
132
 
            }
133
 
        }
134
 
 
135
 
      return found;
136
 
    }
137
 
 
138
 
  grub_pci_iterate (find_card);
139
 
  return found;
140
 
}
141
 
 
142
 
grub_err_t
143
 
grub_xnu_set_video (struct grub_xnu_boot_params *params)
144
 
{
145
 
  grub_efi_uga_draw_protocol_t *c;
146
 
  grub_uint32_t width, height, depth, rate, pixel, fb_base, line_len;
147
 
  int ret;
148
 
 
149
 
  c = grub_efi_locate_protocol (&uga_draw_guid, 0);
150
 
  if (! c)
151
 
    return grub_error (GRUB_ERR_IO, "couldn't find UGADraw");
152
 
 
153
 
  if (efi_call_5 (c->get_mode, c, &width, &height, &depth, &rate))
154
 
    return grub_error (GRUB_ERR_IO, "couldn't retrieve video mode");
155
 
 
156
 
  grub_printf ("Video mode: %ux%u-%u@%u\n", width, height, depth, rate);
157
 
 
158
 
  grub_efi_set_text_mode (0);
159
 
  pixel = RGB_MAGIC;
160
 
  efi_call_10 (c->blt, c, (struct grub_efi_uga_pixel *) &pixel,
161
 
               GRUB_EFI_UGA_VIDEO_FILL, 0, 0, 0, 0, 1, height, 0);
162
 
  ret = find_framebuf (&fb_base, &line_len);
163
 
  grub_efi_set_text_mode (1);
164
 
 
165
 
  if (! ret)
166
 
    return grub_error (GRUB_ERR_IO, "can\'t find frame buffer address");
167
 
 
168
 
  grub_printf ("Frame buffer base: 0x%x\n", fb_base);
169
 
  grub_printf ("Video line length: %d\n", line_len);
170
 
 
171
 
  params->lfb_width = width;
172
 
  params->lfb_height = height;
173
 
  params->lfb_depth = depth;
174
 
  params->lfb_line_len = line_len;
175
 
  params->lfb_mode = GRUB_XNU_VIDEO_TEXT_IN_VIDEO;
176
 
  params->lfb_base = fb_base;
177
 
  return GRUB_ERR_NONE;
178
 
}