~ubuntu-branches/debian/sid/grub2/sid-200907171840

« back to all changes in this revision

Viewing changes to fs/i386/pc/pxe.c

  • Committer: Bazaar Package Importer
  • Author(s): Robert Millan
  • Date: 2009-07-02 13:23:51 UTC
  • mfrom: (1.1.10 upstream)
  • Revision ID: james.westby@ubuntu.com-20090702132351-tanpn0ryyijp93gu
Tags: 1.96+20090702-1
* New SVN snapshot.
* rules: Remove duplicated files in sparc64-ieee1275 port.
* rules: Comment out -DGRUB_ASSUME_LINUX_HAS_FB_SUPPORT=1 setting.  We'll
  re-evaluate using it when it's more mature.  (Closes: #535026).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* pxe.c - Driver to provide access to the pxe filesystem  */
 
2
/*
 
3
 *  GRUB  --  GRand Unified Bootloader
 
4
 *  Copyright (C) 2008  Free Software Foundation, Inc.
 
5
 *
 
6
 *  GRUB is free software: you can redistribute it and/or modify
 
7
 *  it under the terms of the GNU General Public License as published by
 
8
 *  the Free Software Foundation, either version 3 of the License, or
 
9
 *  (at your option) any later version.
 
10
 *
 
11
 *  GRUB is distributed in the hope that it will be useful,
 
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 *  GNU General Public License for more details.
 
15
 *
 
16
 *  You should have received a copy of the GNU General Public License
 
17
 *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
 
18
 */
 
19
 
 
20
#include <grub/dl.h>
 
21
#include <grub/fs.h>
 
22
#include <grub/mm.h>
 
23
#include <grub/disk.h>
 
24
#include <grub/file.h>
 
25
#include <grub/misc.h>
 
26
#include <grub/bufio.h>
 
27
 
 
28
#include <grub/machine/pxe.h>
 
29
#include <grub/machine/memory.h>
 
30
 
 
31
#define SEGMENT(x)      ((x) >> 4)
 
32
#define OFFSET(x)       ((x) & 0xF)
 
33
#define SEGOFS(x)       ((SEGMENT(x) << 16) + OFFSET(x))
 
34
#define LINEAR(x)       (void *) (((x >> 16) <<4) + (x & 0xFFFF))
 
35
 
 
36
struct grub_pxenv *grub_pxe_pxenv;
 
37
grub_uint32_t grub_pxe_your_ip;
 
38
grub_uint32_t grub_pxe_server_ip;
 
39
grub_uint32_t grub_pxe_gateway_ip;
 
40
int grub_pxe_blksize = GRUB_PXE_MIN_BLKSIZE;
 
41
 
 
42
static grub_file_t curr_file = 0;
 
43
 
 
44
struct grub_pxe_data
 
45
{
 
46
  grub_uint32_t packet_number;
 
47
  grub_uint32_t block_size;
 
48
  char filename[0];
 
49
};
 
50
 
 
51
static int
 
52
grub_pxe_iterate (int (*hook) (const char *name))
 
53
{
 
54
  if (hook ("pxe"))
 
55
    return 1;
 
56
  return 0;
 
57
}
 
58
 
 
59
static grub_err_t
 
60
grub_pxe_open (const char *name, grub_disk_t disk)
 
61
{
 
62
  if (grub_strcmp (name, "pxe"))
 
63
      return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "not a pxe disk");
 
64
 
 
65
  disk->total_sectors = 0;
 
66
  disk->id = (unsigned long) "pxe";
 
67
 
 
68
  disk->has_partitions = 0;
 
69
  disk->data = 0;
 
70
 
 
71
  return GRUB_ERR_NONE;
 
72
}
 
73
 
 
74
static void
 
75
grub_pxe_close (grub_disk_t disk __attribute((unused)))
 
76
{
 
77
}
 
78
 
 
79
static grub_err_t
 
80
grub_pxe_read (grub_disk_t disk __attribute((unused)),
 
81
               grub_disk_addr_t sector __attribute((unused)),
 
82
               grub_size_t size __attribute((unused)),
 
83
               char *buf __attribute((unused)))
 
84
{
 
85
  return GRUB_ERR_OUT_OF_RANGE;
 
86
}
 
87
 
 
88
static grub_err_t
 
89
grub_pxe_write (grub_disk_t disk __attribute((unused)),
 
90
                grub_disk_addr_t sector __attribute((unused)),
 
91
                grub_size_t size __attribute((unused)),
 
92
                const char *buf __attribute((unused)))
 
93
{
 
94
  return GRUB_ERR_OUT_OF_RANGE;
 
95
}
 
96
 
 
97
static struct grub_disk_dev grub_pxe_dev =
 
98
  {
 
99
    .name = "pxe",
 
100
    .id = GRUB_DISK_DEVICE_PXE_ID,
 
101
    .iterate = grub_pxe_iterate,
 
102
    .open = grub_pxe_open,
 
103
    .close = grub_pxe_close,
 
104
    .read = grub_pxe_read,
 
105
    .write = grub_pxe_write,
 
106
    .next = 0
 
107
  };
 
108
 
 
109
static grub_err_t
 
110
grub_pxefs_dir (grub_device_t device UNUSED, const char *path UNUSED,
 
111
                int (*hook) (const char *filename,
 
112
                             const struct grub_dirhook_info *info) UNUSED)
 
113
{
 
114
  return GRUB_ERR_NONE;
 
115
}
 
116
 
 
117
static grub_err_t
 
118
grub_pxefs_open (struct grub_file *file, const char *name)
 
119
{
 
120
  union
 
121
    {
 
122
      struct grub_pxenv_tftp_get_fsize c1;
 
123
      struct grub_pxenv_tftp_open c2;
 
124
    } c;
 
125
  struct grub_pxe_data *data;
 
126
  grub_file_t file_int, bufio;
 
127
 
 
128
  if (curr_file != 0)
 
129
    {
 
130
      grub_pxe_call (GRUB_PXENV_TFTP_CLOSE, &c.c2);
 
131
      curr_file = 0;
 
132
    }
 
133
 
 
134
  c.c1.server_ip = grub_pxe_server_ip;
 
135
  c.c1.gateway_ip = grub_pxe_gateway_ip;
 
136
  grub_strcpy ((char *)&c.c1.filename[0], name);
 
137
  grub_pxe_call (GRUB_PXENV_TFTP_GET_FSIZE, &c.c1);
 
138
  if (c.c1.status)
 
139
    return grub_error (GRUB_ERR_FILE_NOT_FOUND, "file not found");
 
140
 
 
141
  file->size = c.c1.file_size;
 
142
 
 
143
  c.c2.tftp_port = grub_cpu_to_be16 (GRUB_PXE_TFTP_PORT);
 
144
  c.c2.packet_size = grub_pxe_blksize;
 
145
  grub_pxe_call (GRUB_PXENV_TFTP_OPEN, &c.c2);
 
146
  if (c.c2.status)
 
147
    return grub_error (GRUB_ERR_BAD_FS, "open fails");
 
148
 
 
149
  data = grub_malloc (sizeof (struct grub_pxe_data) + grub_strlen (name) + 1);
 
150
  if (! data)
 
151
    return grub_errno;
 
152
 
 
153
  data->packet_number = 0;
 
154
  data->block_size = grub_pxe_blksize;
 
155
  grub_strcpy (data->filename, name);
 
156
 
 
157
  file_int = grub_malloc (sizeof (*file_int));
 
158
  if (! file_int)
 
159
    {
 
160
      grub_free (data);
 
161
      return grub_errno;
 
162
    }
 
163
 
 
164
  file->data = data;
 
165
  grub_memcpy (file_int, file, sizeof (struct grub_file));
 
166
  curr_file = file_int;
 
167
 
 
168
  bufio = grub_bufio_open (file_int, data->block_size);
 
169
  if (! bufio)
 
170
    {
 
171
      grub_free (file_int);
 
172
      grub_free (data);
 
173
      return grub_errno;
 
174
    }
 
175
 
 
176
  grub_memcpy (file, bufio, sizeof (struct grub_file));
 
177
 
 
178
  return GRUB_ERR_NONE;
 
179
}
 
180
 
 
181
static grub_ssize_t
 
182
grub_pxefs_read (grub_file_t file, char *buf, grub_size_t len)
 
183
{
 
184
  struct grub_pxenv_tftp_read c;
 
185
  struct grub_pxe_data *data;
 
186
  grub_uint32_t pn, r;
 
187
 
 
188
  data = file->data;
 
189
 
 
190
  pn = grub_divmod64 (file->offset, data->block_size, &r);
 
191
  if (r)
 
192
    {
 
193
      grub_error (GRUB_ERR_BAD_FS,
 
194
                  "read access must be aligned to packet size");
 
195
      return -1;
 
196
    }
 
197
 
 
198
  if ((curr_file != file) || (data->packet_number > pn))
 
199
    {
 
200
      struct grub_pxenv_tftp_open o;
 
201
 
 
202
      if (curr_file != 0)
 
203
        grub_pxe_call (GRUB_PXENV_TFTP_CLOSE, &o);
 
204
 
 
205
      o.server_ip = grub_pxe_server_ip;
 
206
      o.gateway_ip = grub_pxe_gateway_ip;
 
207
      grub_strcpy ((char *)&o.filename[0], data->filename);
 
208
      o.tftp_port = grub_cpu_to_be16 (GRUB_PXE_TFTP_PORT);
 
209
      o.packet_size = data->block_size;
 
210
      grub_pxe_call (GRUB_PXENV_TFTP_OPEN, &o);
 
211
      if (o.status)
 
212
        {
 
213
          grub_error (GRUB_ERR_BAD_FS, "open fails");
 
214
          return -1;
 
215
        }
 
216
      data->packet_number = 0;
 
217
      curr_file = file;
 
218
    }
 
219
 
 
220
  c.buffer = SEGOFS (GRUB_MEMORY_MACHINE_SCRATCH_ADDR);
 
221
  while (pn >= data->packet_number)
 
222
    {
 
223
      c.buffer_size = grub_pxe_blksize;
 
224
      grub_pxe_call (GRUB_PXENV_TFTP_READ, &c);
 
225
      if (c.status)
 
226
        {
 
227
          grub_error (GRUB_ERR_BAD_FS, "read fails");
 
228
          return -1;
 
229
        }
 
230
      data->packet_number++;
 
231
    }
 
232
 
 
233
  grub_memcpy (buf, (char *) GRUB_MEMORY_MACHINE_SCRATCH_ADDR, len);
 
234
 
 
235
  return len;
 
236
}
 
237
 
 
238
static grub_err_t
 
239
grub_pxefs_close (grub_file_t file)
 
240
{
 
241
  struct grub_pxenv_tftp_close c;
 
242
 
 
243
  if (curr_file == file)
 
244
    {
 
245
      grub_pxe_call (GRUB_PXENV_TFTP_CLOSE, &c);
 
246
      curr_file = 0;
 
247
    }
 
248
 
 
249
  grub_free (file->data);
 
250
 
 
251
  return GRUB_ERR_NONE;
 
252
}
 
253
 
 
254
static grub_err_t
 
255
grub_pxefs_label (grub_device_t device __attribute ((unused)),
 
256
                   char **label __attribute ((unused)))
 
257
{
 
258
  *label = 0;
 
259
  return GRUB_ERR_NONE;
 
260
}
 
261
 
 
262
static struct grub_fs grub_pxefs_fs =
 
263
  {
 
264
    .name = "pxefs",
 
265
    .dir = grub_pxefs_dir,
 
266
    .open = grub_pxefs_open,
 
267
    .read = grub_pxefs_read,
 
268
    .close = grub_pxefs_close,
 
269
    .label = grub_pxefs_label,
 
270
    .next = 0
 
271
  };
 
272
 
 
273
static void
 
274
grub_pxe_detect (void)
 
275
{
 
276
  struct grub_pxenv *pxenv;
 
277
  struct grub_pxenv_get_cached_info ci;
 
278
  struct grub_pxenv_boot_player *bp;
 
279
 
 
280
  pxenv = grub_pxe_scan ();
 
281
  if (! pxenv)
 
282
    return;
 
283
 
 
284
  ci.packet_type = GRUB_PXENV_PACKET_TYPE_DHCP_ACK;
 
285
  ci.buffer = 0;
 
286
  ci.buffer_size = 0;
 
287
  grub_pxe_call (GRUB_PXENV_GET_CACHED_INFO, &ci);
 
288
  if (ci.status)
 
289
    return;
 
290
 
 
291
  bp = LINEAR (ci.buffer);
 
292
 
 
293
  grub_pxe_your_ip = bp->your_ip;
 
294
  grub_pxe_server_ip = bp->server_ip;
 
295
  grub_pxe_gateway_ip = bp->gateway_ip;
 
296
 
 
297
  grub_pxe_pxenv = pxenv;
 
298
}
 
299
 
 
300
void
 
301
grub_pxe_unload (void)
 
302
{
 
303
  if (grub_pxe_pxenv)
 
304
    {
 
305
      grub_fs_unregister (&grub_pxefs_fs);
 
306
      grub_disk_dev_unregister (&grub_pxe_dev);
 
307
 
 
308
      grub_pxe_pxenv = 0;
 
309
    }
 
310
}
 
311
 
 
312
GRUB_MOD_INIT(pxe)
 
313
{
 
314
  grub_pxe_detect ();
 
315
  if (grub_pxe_pxenv)
 
316
    {
 
317
      grub_disk_dev_register (&grub_pxe_dev);
 
318
      grub_fs_register (&grub_pxefs_fs);
 
319
    }
 
320
}
 
321
 
 
322
GRUB_MOD_FINI(pxe)
 
323
{
 
324
  grub_pxe_unload ();
 
325
}