aboutsummaryrefslogtreecommitdiff
blob: da039143790b6b852a01fec5986e130ef596a889 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#
# edd.py
# BIOS EDD data parsing functions
#
# Copyright (C) 2010  Red Hat, Inc.  All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Author(s): Hans de Goede <hdegoede@redhat.com>
#

import os
import struct

import logging
log = logging.getLogger("storage")

def get_edd_dict(devices):
    """Given an array of devices return a dict with the BIOS ID for them."""
    edd_dict = {}

    for biosdev in range(80, 80 + 15):
        sysfspath = "/sys/firmware/edd/int13_dev%d" % biosdev
        if not os.path.exists(sysfspath):
            break # We are done

        sysfspath = "/sys/firmware/edd/int13_dev%d/mbr_signature" % biosdev
        if not os.path.exists(sysfspath):
            log.warning("No mbrsig for biosdev: %d" % biosdev)
            continue

        try:
            file = open(sysfspath, "r")
            eddsig = file.read()
            file.close()
        except (IOError, OSError) as e:
            log.warning("Error reading EDD mbrsig for %d: %s" %
                        (biosdev, str(e)))
            continue

        sysfspath = "/sys/firmware/edd/int13_dev%d/sectors" % biosdev
        try:
            file = open(sysfspath, "r")
            eddsize = file.read()
            file.close()
        except (IOError, OSError) as e:
            eddsize = None

        found = []
        for dev in devices:
            try:
                fd = os.open(dev.path, os.O_RDONLY)
                os.lseek(fd, 440, 0) 
                mbrsig = struct.unpack('I', os.read(fd, 4))
                os.close(fd)
            except OSError as e:
                log.warning("Error reading mbrsig from disk %s: %s" %
                            (dev.name, str(e)))
                continue

            mbrsigStr = "0x%08x\n" % mbrsig
            if mbrsigStr == eddsig:
                if eddsize:
                    sysfspath = "/sys%s/size" % dev.sysfsPath
                    try:
                        file = open(sysfspath, "r")
                        size = file.read()
                        file.close()
                    except (IOError, OSError) as e:
                        log.warning("Error getting size for: %s" % dev.name)
                        continue
                    if eddsize != size:
                        continue
                found.append(dev.name)

        if not found:
            log.error("No matching mbr signature found for biosdev %d" %
                      biosdev)
        elif len(found) > 1:
            log.error("Multiple signature matches found for biosdev %d: %s" %
                      (biosdev, str(found)))
        else:
            log.info("Found %s for biosdev %d" %(found[0], biosdev))
            edd_dict[found[0]] = biosdev

    return edd_dict