~darkmuggle-deactivatedaccount/ubuntu/quantal/grub2/fix-872244

« back to all changes in this revision

Viewing changes to kern/device.c

  • Committer: Bazaar Package Importer
  • Author(s): Otavio Salvador
  • Date: 2006-01-05 15:20:40 UTC
  • mto: (17.3.1 squeeze) (1.9.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: james.westby@ubuntu.com-20060105152040-b72i5pq1a82z22yi
Tags: upstream-1.92
ImportĀ upstreamĀ versionĀ 1.92

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* device.c - device manager */
 
2
/*
 
3
 *  GRUB  --  GRand Unified Bootloader
 
4
 *  Copyright (C) 2002,2005  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 2 of the License, or
 
9
 *  (at your option) any later version.
 
10
 *
 
11
 *  This program 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, write to the Free Software
 
18
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
19
 */
 
20
 
 
21
#include <grub/device.h>
 
22
#include <grub/disk.h>
 
23
#include <grub/net.h>
 
24
#include <grub/fs.h>
 
25
#include <grub/mm.h>
 
26
#include <grub/misc.h>
 
27
#include <grub/env.h>
 
28
#include <grub/partition.h>
 
29
 
 
30
grub_device_t
 
31
grub_device_open (const char *name)
 
32
{
 
33
  grub_disk_t disk = 0;
 
34
  grub_device_t dev = 0;
 
35
 
 
36
  if (! name)
 
37
    {
 
38
      name = grub_env_get ("root");
 
39
      if (*name == '\0')
 
40
        {
 
41
          grub_error (GRUB_ERR_BAD_DEVICE, "no device is set");
 
42
          goto fail;
 
43
        }
 
44
    }
 
45
    
 
46
  dev = grub_malloc (sizeof (*dev));
 
47
  if (! dev)
 
48
    goto fail;
 
49
  
 
50
  /* Try to open a disk.  */
 
51
  disk = grub_disk_open (name);
 
52
  if (! disk)
 
53
    {
 
54
      grub_error (GRUB_ERR_BAD_DEVICE, "unknown device");
 
55
      goto fail;
 
56
    }
 
57
 
 
58
  dev->disk = disk;
 
59
  dev->net = 0; /* FIXME */
 
60
 
 
61
  return dev;
 
62
 
 
63
 fail:
 
64
  if (disk)
 
65
    grub_disk_close (disk);
 
66
  
 
67
  grub_free (dev);
 
68
 
 
69
  return 0;
 
70
}
 
71
 
 
72
grub_err_t
 
73
grub_device_close (grub_device_t device)
 
74
{
 
75
  if (device->disk)
 
76
    grub_disk_close (device->disk);
 
77
 
 
78
  grub_free (device);
 
79
 
 
80
  return grub_errno;
 
81
}
 
82
 
 
83
int
 
84
grub_device_iterate (int (*hook) (const char *name))
 
85
{
 
86
  auto int iterate_disk (const char *disk_name);
 
87
  auto int iterate_partition (grub_disk_t disk,
 
88
                              const grub_partition_t partition);
 
89
  
 
90
  int iterate_disk (const char *disk_name)
 
91
    {
 
92
      grub_device_t dev;
 
93
 
 
94
      if (hook (disk_name))
 
95
        return 1;
 
96
      
 
97
      dev = grub_device_open (disk_name);
 
98
      if (! dev)
 
99
        return 1;
 
100
      
 
101
      if (dev->disk && dev->disk->has_partitions)
 
102
        if (grub_partition_iterate (dev->disk, iterate_partition))
 
103
          {
 
104
            grub_device_close (dev);
 
105
            return 1;
 
106
          }
 
107
 
 
108
      grub_device_close (dev);
 
109
      return 0;
 
110
    }
 
111
  
 
112
  int iterate_partition (grub_disk_t disk, const grub_partition_t partition)
 
113
    {
 
114
      char *partition_name;
 
115
      char *device_name;
 
116
      int ret;
 
117
      
 
118
      partition_name = grub_partition_get_name (partition);
 
119
      if (! partition_name)
 
120
        return 1;
 
121
      
 
122
      device_name = grub_malloc (grub_strlen (disk->name) + 1
 
123
                                 + grub_strlen (partition_name) + 1);
 
124
      if (! device_name)
 
125
        {
 
126
          grub_free (partition_name);
 
127
          return 1;
 
128
        }
 
129
 
 
130
      grub_sprintf (device_name, "%s,%s", disk->name, partition_name);
 
131
      grub_free (partition_name);
 
132
 
 
133
      ret = hook (device_name);
 
134
      grub_free (device_name);
 
135
      return ret;
 
136
    }
 
137
 
 
138
  /* Only disk devices are supported at the moment.  */
 
139
  return grub_disk_dev_iterate (iterate_disk);
 
140
}