/* * Copyright 2009 Jasmin Fazlic All rights reserved. */ /* * Cleaned and merged by iNDi */ #include "config.h" #include "libsaio.h" #include "boot.h" #include "bootstruct.h" #include "pci.h" #include "pci_root.h" #include "device_inject.h" #include "convert.h" #if DEBUG_INJECT #define DBG(x...) printf(x) #else #define DBG(x...) msglog(x) #endif uint32_t devices_number = 1; // nvidia.c uint32_t location_number = 1; // networking.c DevPropString *string = NULL; uint8_t *stringdata = NULL; uint32_t stringlength = 0; char *efi_inject_get_devprop_string(uint32_t *len) { if(string) { *len = string->length; return devprop_generate_string(string); } verbose("efi_inject_get_devprop_string NULL trying stringdata\n"); return NULL; } void setupDeviceProperties(Node *node) { const char *val; uint8_t *binStr; int cnt, cnt2; static char DEVICE_PROPERTIES_PROP[] = "device-properties"; /* Generate devprop string. */ uint32_t strlength; char *string = efi_inject_get_devprop_string(&strlength); /* Use the static "device-properties" boot config key contents if available, * otheriwse use the generated one. */ if (!getValueForKey(kDeviceProperties, &val, &cnt, &bootInfo->chameleonConfig) && string) { val = (const char *)string; cnt = strlength * 2; } if (cnt > 1) { binStr = convertHexStr2Binary(val, &cnt2); if (cnt2 > 0) { DT__AddProperty(node, DEVICE_PROPERTIES_PROP, cnt2, binStr); DBG("Adding device-properties string to DT\n"); } } } DevPropString *devprop_create_string(void) { string = (DevPropString *)malloc(sizeof(DevPropString)); if(string == NULL) { return NULL; } memset(string, 0, sizeof(DevPropString)); string->length = 12; string->WHAT2 = 0x01000000; return string; } DevPropDevice *devprop_add_device(DevPropString *string, char *path) { DevPropDevice *device = NULL; const char pciroot_string[] = "PciRoot(0x"; const char pci_device_string[] = "Pci(0x"; if (string == NULL || path == NULL) { printf("ERROR null device path\n"); return NULL; } if (strncmp(path, pciroot_string, strlen(pciroot_string))) { printf("ERROR parsing device path\n"); return NULL; } if (!(device = malloc(sizeof(DevPropDevice)))) { printf("ERROR malloc failed\n"); return NULL; } memset(device, 0, sizeof(DevPropDevice)); device->acpi_dev_path._UID = getPciRootUID(); int numpaths = 0; int x, curr = 0; char buff[] = "00"; for (x = 0; x < strlen(path); x++) { if (!strncmp(&path[x], pci_device_string, strlen(pci_device_string))) { x+=strlen(pci_device_string); curr=x; while(path[++x] != ','); if(x-curr == 2) { sprintf(buff, "%c%c", path[curr], path[curr+1]); } else if(x-curr == 1) { sprintf(buff, "%c", path[curr]); } else { printf("ERROR parsing device path\n"); numpaths = 0; break; } device->pci_dev_path[numpaths].device = ascii_hex_to_int(buff); x += 3; // 0x curr = x; while(path[++x] != ')'); if(x-curr == 2) { sprintf(buff, "%c%c", path[curr], path[curr+1]); } else if(x-curr == 1) { sprintf(buff, "%c", path[curr]); } else { printf("ERROR parsing device path\n"); numpaths = 0; break; } device->pci_dev_path[numpaths].function = ascii_hex_to_int(buff); // TODO: find dev from char *path numpaths++; } } if(!numpaths) { free(device); return NULL; } device->numentries = 0x00; device->acpi_dev_path.length = 0x0c; device->acpi_dev_path.type = 0x02; device->acpi_dev_path.subtype = 0x01; device->acpi_dev_path._HID = 0xd041030a; // 0x0a0341d0 device->num_pci_devpaths = numpaths; device->length = 24 + (6*numpaths); int i; for(i = 0; i < numpaths; i++) { device->pci_dev_path[i].length = 0x06; device->pci_dev_path[i].type = 0x01; device->pci_dev_path[i].subtype = 0x01; } device->path_end.length = 0x04; device->path_end.type = 0x7f; device->path_end.subtype = 0xff; device->string = string; device->data = NULL; if(!string->entries) { if (!(string->entries = (struct DevPropDevice**) malloc(sizeof(device) * DEV_PROP_DEVICE_MAX_ENTRIES))) { free(device); return NULL; } } /* FIXME: probably needs bounds checking, as well as error handling in event of malloc failure */ string->length += device->length; string->entries[string->numentries++] = (DevPropDevice*)malloc(sizeof(device)); string->entries[string->numentries-1] = device; return device; } int devprop_add_value(DevPropDevice *device, char *nm, uint8_t *vl, uint32_t len) { if(!nm || !vl || !len) { return 0; } uint32_t length = ((strlen(nm) * 2) + len + (2 * sizeof(uint32_t)) + 2); uint8_t *data = (uint8_t*)malloc(length); if(!data) { return 0; } memset(data, 0, length); uint32_t off = 0; data[off+1] = (uint8_t)(((strlen(nm) * 2) + 6) >> 8); data[off] = ((strlen(nm) * 2) + 6) & 0x00FF; off += 4; uint32_t i=0, l = strlen(nm); for(i = 0 ; i < l ; i++, off += 2) { data[off] = *nm++; } off += 2; l = len; uint32_t *datalength = (uint32_t*)&data[off]; *datalength = (uint32_t)(l + 4); off += 4; for(i = 0 ; i < l ; i++, off++) { data[off] = *vl++; } uint32_t offset = device->length - (24 + (6 * device->num_pci_devpaths)); uint8_t *newdata = (uint8_t*)malloc((length + offset)); if(!newdata) { return 0; } if(device->data) { if(offset > 1) { memcpy(newdata, device->data, offset); } } memcpy(newdata + offset, data, length); device->length += length; device->string->length += length; device->numentries++; if(!device->data) { device->data = (uint8_t *)malloc(sizeof(uint8_t)); } else { free(device->data); } free(data); device->data = newdata; return 1; } // devprop_generate_string optimized by cparm char *devprop_generate_string(DevPropString *string) { int len = ((string->length * 2) + 1); char *buffer = (char *)malloc(len); char *ptr = buffer; if(!buffer) { return NULL; } snprintf(buffer, len, "%08x%08x%04x%04x", dp_swap32(string->length), string->WHAT2, dp_swap16(string->numentries), string->WHAT3); buffer += 24; len -= 24; int i = 0, x = 0; while(i < string->numentries) { if (!(i < DEV_PROP_DEVICE_MAX_ENTRIES)) { break; } if(!len) { break; } snprintf(buffer, len, "%08x%04x%04x", dp_swap32(string->entries[i]->length), dp_swap16(string->entries[i]->numentries), string->entries[i]->WHAT2); buffer += 16; len -= 16; snprintf(buffer, len, "%02x%02x%04x%08x%08x", string->entries[i]->acpi_dev_path.type, string->entries[i]->acpi_dev_path.subtype, dp_swap16(string->entries[i]->acpi_dev_path.length), string->entries[i]->acpi_dev_path._HID, dp_swap32(string->entries[i]->acpi_dev_path._UID)); buffer += 24; len -= 24; for(x = 0;x < string->entries[i]->num_pci_devpaths; x++) { if(!len) { break; } snprintf(buffer, len, "%02x%02x%04x%02x%02x", string->entries[i]->pci_dev_path[x].type, string->entries[i]->pci_dev_path[x].subtype, dp_swap16(string->entries[i]->pci_dev_path[x].length), string->entries[i]->pci_dev_path[x].function, string->entries[i]->pci_dev_path[x].device); buffer += 12; len -= 12; } if(!len) { break; } snprintf(buffer, len, "%02x%02x%04x", string->entries[i]->path_end.type, string->entries[i]->path_end.subtype, dp_swap16(string->entries[i]->path_end.length)); buffer += 8; len -= 8; uint8_t *dataptr = string->entries[i]->data; for(x = 0; (uint32_t)x < (string->entries[i]->length) - (24 + (6 * string->entries[i]->num_pci_devpaths)) ; x++) { if(!len) { break; } snprintf(buffer, len, "%02x", *dataptr++); buffer += 2; len -= 2; } i++; } return ptr; } void devprop_free_string(DevPropString *string) { if(!string) { return; } int i; for(i = 0; i < string->numentries; i++) { if(string->entries[i]) { if(string->entries[i]->data) { free(string->entries[i]->data); string->entries[i]->data = NULL; } free(string->entries[i]); string->entries[i] = NULL; } } free(string); string = NULL; } /* ======================================================= */ /* a fine place for this code */ // (devprop_add_network_template) and (setup_eth_builtin) moved to i386/libsaio/networking.c