1 /* 2 * Copyright 2009, Colin Günther, coling@gmx.de. 3 * All Rights Reserved. Distributed under the terms of the MIT License. 4 * 5 */ 6 7 8 #include <compat/sys/param.h> 9 #include <compat/sys/firmware.h> 10 #include <compat/sys/haiku-module.h> 11 12 #include <stdlib.h> 13 #include <string.h> 14 15 #include <FindDirectory.h> 16 #include <StorageDefs.h> 17 #include <SupportDefs.h> 18 19 20 const struct firmware* 21 firmware_get(const char* imageName) 22 { 23 int fileDescriptor; 24 struct firmware* firmware = NULL; 25 int32 firmwareFileSize; 26 char* firmwareName; 27 char* firmwarePath; 28 ssize_t readCount; 29 30 firmwarePath = (char*)malloc(B_PATH_NAME_LENGTH); 31 if (firmwarePath == NULL) 32 goto cleanup0; 33 34 if (find_directory(B_SYSTEM_DATA_DIRECTORY, -1, false, 35 firmwarePath, B_PATH_NAME_LENGTH) != B_OK) 36 goto cleanup1; 37 38 strlcat(firmwarePath, "/firmware/", B_PATH_NAME_LENGTH); 39 strlcat(firmwarePath, gDriverName, B_PATH_NAME_LENGTH); 40 strlcat(firmwarePath, "/", B_PATH_NAME_LENGTH); 41 strlcat(firmwarePath, imageName, B_PATH_NAME_LENGTH); 42 43 fileDescriptor = open(firmwarePath, B_READ_ONLY); 44 if (fileDescriptor == -1) 45 goto cleanup1; 46 47 firmwareFileSize = lseek(fileDescriptor, 0, SEEK_END); 48 lseek(fileDescriptor, 0, SEEK_SET); 49 50 firmwareName = (char*)malloc(strlen(imageName) + 1); 51 if (firmwareName == NULL) 52 goto cleanup2; 53 54 strncpy(firmwareName, imageName, strlen(imageName) + 1); 55 56 firmware = (struct firmware*)malloc(sizeof(struct firmware)); 57 if (firmware == NULL) 58 goto cleanup3; 59 60 firmware->data = malloc(firmwareFileSize); 61 if (firmware->data == NULL) 62 goto cleanup4; 63 64 readCount = read(fileDescriptor, (void*)firmware->data, firmwareFileSize); 65 if (readCount == -1) 66 goto cleanup5; 67 68 firmware->datasize = firmwareFileSize; 69 firmware->name = firmwareName; 70 firmware->version = __haiku_firmware_version; 71 goto cleanup2; 72 73 cleanup5: 74 free(&firmware->data); 75 cleanup4: 76 free(firmware); 77 firmware = NULL; 78 cleanup3: 79 free(firmwareName); 80 cleanup2: 81 close(fileDescriptor); 82 cleanup1: 83 free(firmwarePath); 84 cleanup0: 85 return firmware; 86 } 87 88 89 void 90 firmware_put(const struct firmware* pointer, int flags) 91 { 92 93 } 94