]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - probe_roms.c
Create.c: fix uclibc build
[thirdparty/mdadm.git] / probe_roms.c
index 0f0ffbccbdc8c1bb14ce158042c61d7ce658743f..94c80c2cc6d0f24fa4c9a370608f2d130006636b 100644 (file)
@@ -20,8 +20,8 @@
  */
 
 #include "probe_roms.h"
+#include "mdadm.h"
 #include <unistd.h>
-#include <signal.h>
 #include <fcntl.h>
 #include <sys/mman.h>
 #include <sys/stat.h>
 
 static void *rom_mem = MAP_FAILED;
 static int rom_fd = -1;
-const static int rom_len = 0xf0000 - 0xc0000; /* option-rom memory region */
+static const int rom_len = 0xf0000 - 0xc0000; /* option-rom memory region */
 static int _sigbus;
 static unsigned long rom_align;
 
-#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
+static void roms_deinit(void);
+static int roms_init(void);
 
 static void sigbus(int sig)
 {
@@ -67,7 +68,8 @@ static int probe_address16(const __u16 *ptr, __u16 *val)
 
 void probe_roms_exit(void)
 {
-       signal(SIGBUS, SIG_DFL);
+       signal_s(SIGBUS, SIG_DFL);
+
        if (rom_fd >= 0) {
                close(rom_fd);
                rom_fd = -1;
@@ -76,6 +78,7 @@ void probe_roms_exit(void)
                munmap(rom_mem, rom_len);
                rom_mem = MAP_FAILED;
        }
+       roms_deinit();
 }
 
 int probe_roms_init(unsigned long align)
@@ -92,7 +95,10 @@ int probe_roms_init(unsigned long align)
        else
                return -1;
 
-       if (signal(SIGBUS, sigbus) == SIG_ERR)
+       if (roms_init())
+               return -1;
+
+       if (signal_s(SIGBUS, sigbus) == SIG_ERR)
                rc = -1;
        if (rc == 0) {
                fd = open("/dev/mem", O_RDONLY);
@@ -108,7 +114,7 @@ int probe_roms_init(unsigned long align)
        if (rc == 0)
                rom_fd = fd;
        else {
-               if (fd >= 0) 
+               if (fd >= 0)
                        close(fd);
                probe_roms_exit();
        }
@@ -130,55 +136,63 @@ static void *isa_bus_to_virt(unsigned long addr)
 struct resource {
        unsigned long start;
        unsigned long end;
+       unsigned long data;
        const char *name;
+       struct resource *next;
 };
 
 static struct resource system_rom_resource = {
        .name   = "System ROM",
        .start  = 0xf0000,
+       .data   = 0,
        .end    = 0xfffff,
 };
 
 static struct resource extension_rom_resource = {
        .name   = "Extension ROM",
        .start  = 0xe0000,
+       .data   = 0,
        .end    = 0xeffff,
 };
 
-static struct resource adapter_rom_resources[] = { {
-       .name   = "Adapter ROM",
-       .start  = 0xc8000,
-       .end    = 0,
-}, {
-       .name   = "Adapter ROM",
-       .start  = 0,
-       .end    = 0,
-}, {
-       .name   = "Adapter ROM",
-       .start  = 0,
-       .end    = 0,
-}, {
-       .name   = "Adapter ROM",
-       .start  = 0,
-       .end    = 0,
-}, {
-       .name   = "Adapter ROM",
-       .start  = 0,
-       .end    = 0,
-}, {
-       .name   = "Adapter ROM",
-       .start  = 0,
-       .end    = 0,
-} };
+static struct resource *adapter_rom_resources;
 
 static struct resource video_rom_resource = {
-       .name   = "Video ROM",
+       .name   = "Video ROM",
        .start  = 0xc0000,
+       .data   = 0,
        .end    = 0xc7fff,
 };
 
+static int roms_init(void)
+{
+       adapter_rom_resources = malloc(sizeof(struct resource));
+       if (adapter_rom_resources == NULL)
+               return 1;
+       adapter_rom_resources->name = "Adapter ROM";
+       adapter_rom_resources->start = 0xc8000;
+       adapter_rom_resources->data = 0;
+       adapter_rom_resources->end = 0;
+       adapter_rom_resources->next = NULL;
+       return 0;
+}
+
+static void roms_deinit(void)
+{
+       struct resource *res;
+
+       res = adapter_rom_resources;
+       while (res) {
+               struct resource *tmp = res;
+
+               res = res->next;
+               free(tmp);
+       }
+}
+
 #define ROMSIGNATURE 0xaa55
 
+
 static int romsignature(const unsigned char *rom)
 {
        const unsigned short * const ptr = (const unsigned short *)rom;
@@ -199,23 +213,23 @@ static int romchecksum(const unsigned char *rom, unsigned long length)
 int scan_adapter_roms(scan_fn fn)
 {
        /* let scan_fn examing each of the adapter roms found by probe_roms */
-       int i;
+       struct resource *res = adapter_rom_resources;
        int found;
 
        if (rom_fd < 0)
                return 0;
 
        found = 0;
-       for (i = 0; i < ARRAY_SIZE(adapter_rom_resources); i++) {
-               struct resource *res = &adapter_rom_resources[i];
-
+       while (res) {
                if (res->start) {
                        found = fn(isa_bus_to_virt(res->start),
-                                  isa_bus_to_virt(res->end));
+                                  isa_bus_to_virt(res->end),
+                                  isa_bus_to_virt(res->data));
                        if (found)
                                break;
                } else
                        break;
+               res = res->next;
        }
 
        return found;
@@ -231,13 +245,14 @@ void probe_roms(void)
        const void *rom;
        unsigned long start, length, upper;
        unsigned char c;
-       int i;
+       struct resource *res = adapter_rom_resources;
+       __u16 val=0;
 
        if (rom_fd < 0)
                return;
 
        /* video rom */
-       upper = adapter_rom_resources[0].start;
+       upper = res->start;
        for (start = video_rom_resource.start; start < upper; start += rom_align) {
                rom = isa_bus_to_virt(start);
                if (!romsignature(rom))
@@ -272,8 +287,9 @@ void probe_roms(void)
                        upper = extension_rom_resource.start;
        }
 
+       struct resource *prev_res = res;
        /* check for adapter roms on 2k boundaries */
-       for (i = 0; i < ARRAY_SIZE(adapter_rom_resources) && start < upper; start += rom_align) {
+       for (; start < upper; start += rom_align) {
                rom = isa_bus_to_virt(start);
                if (!romsignature(rom))
                        continue;
@@ -284,14 +300,32 @@ void probe_roms(void)
                /* 0 < length <= 0x7f * 512, historically */
                length = c * 512;
 
+               /* Retrieve 16-bit pointer to PCI Data Structure (offset 18h-19h)
+                * The data can be within 64KB forward of the first location
+                * of this code image. The pointer is in little-endian order
+                */
+
+               if (probe_address16(rom + 0x18, &val) != 0)
+                       continue;
+               val = __le16_to_cpu(val);
+
                /* but accept any length that fits if checksum okay */
                if (!length || start + length > upper || !romchecksum(rom, length))
                        continue;
 
-               adapter_rom_resources[i].start = start;
-               adapter_rom_resources[i].end = start + length - 1;
+               if (res == NULL) {
+                       res = calloc(1, sizeof(struct resource));
+                       if (res == NULL)
+                               return;
+                       prev_res->next = res;
+               }
 
-               start = adapter_rom_resources[i++].end & ~(rom_align - 1);
+               res->start = start;
+               res->data = start + (unsigned long)val;
+               res->end = start + length - 1;
+
+               start = res->end & ~(rom_align - 1);
+               prev_res = res;
+               res = res->next;
        }
 }
-