Ticket #5137: boot_celf.patch
File boot_celf.patch, 18.1 KB (added by , 14 years ago) |
---|
-
build/jam/ImageRules
1299 1299 #$(RM) -R $(HAIKU_OUTPUT_DIR)/cd 1300 1300 } 1301 1301 1302 # warning: that is x86 and linux host machine only! 1302 1303 1304 rule CompressFile 1305 { 1306 # CompressFile <file> ; 1307 # Compress file in-place 1308 1309 ALWAYS $(1) ; 1310 CompressFile1 $(1) ; 1311 } 1312 1313 actions CompressFile1 1314 { 1315 echo "$(HAIKU_TOP)/generated/objects/linux/$(TARGET_ARCH)/release/bin/recompress/recompres $(HAIKU_TOP)/$(1) $(HAIKU_TOP)/$(1)" >> $(HAIKU_OUTPUT_DIR)/haiku.image-copy-files 1316 } 1317 1318 rule AddCompressedDriversToHaikuImage 1319 { 1320 # AddCompressedDriversToHaikuImage <drivers> ; 1321 # Compress <drivers> and add compressed <drivers> to ${tPrefix}system/add-ons/kernel/boot 1322 1323 ALWAYS $(1) ; 1324 AddCompressedDriversToHaikuImage1 $(1) ; 1325 } 1326 1327 actions AddCompressedDriversToHaikuImage1 1328 { 1329 for target in $(1) 1330 do 1331 echo "$(HAIKU_TOP)/generated/objects/linux/$(TARGET_ARCH)/release/bin/recompress/recompres $(HAIKU_TOP)/$target $(HAIKU_TOP)/$target.celf" >> $(HAIKU_OUTPUT_DIR)/haiku.image-copy-files 1332 echo "\$cp \"\${sPrefix}$target.celf\" \"\${tPrefix}system/add-ons/kernel/boot\"" >> $(HAIKU_OUTPUT_DIR)/haiku.image-copy-files 1333 done 1334 } -
build/jam/HaikuImage
49 49 passwd paste patch pathchk pc ping play playfile playsound playwav pr prio 50 50 printenv printf profile ps ptx pwd 51 51 query quit 52 rc readlink re index release renice rlog rm rmattr rmindex rmdir roster52 rc readlink recompress reindex release renice rlog rm rmattr rmindex rmdir roster 53 53 route 54 54 safemode screen_blanker screenmode sed setdecor settype setversion setvolume 55 55 seq sha1sum shar shred shuf shutdown sleep sort spamdbm split stat strace … … 242 242 #AddDriversToHaikuImage power : $(SYSTEM_ADD_ONS_DRIVERS_POWER) ; 243 243 244 244 # kernel 245 CompressFile <revisioned>kernel_$(TARGET_ARCH) ; 245 246 AddFilesToHaikuImage system : <revisioned>kernel_$(TARGET_ARCH) ; 246 247 247 248 # libs … … 474 475 # boot loader 475 476 AddFilesToHaikuImage system : haiku_loader ; 476 477 477 # boot module links 478 AddBootModuleSymlinksToHaikuImage 478 # boot modules 479 AddDirectoryToHaikuImage system add-ons kernel boot ; 480 AddCompressedDriversToHaikuImage 479 481 $(X86_ONLY)acpi $(ATA_ONLY)ata pci $(X86_ONLY)isa config_manager dpc 480 482 $(IDE_ONLY)ide scsi usb 481 483 $(PPC_ONLY)openpic -
src/system/boot/loader/elf.cpp
16 16 #include <unistd.h> 17 17 #include <string.h> 18 18 #include <stdlib.h> 19 #include <zlib.h> 19 20 20 21 //#define TRACE_ELF 21 22 #ifdef TRACE_ELF … … 28 29 static bool sLoadElfSymbols = false; 29 30 30 31 32 /* Decompress from file fd to memory dest until EOF. 33 inflater() returns B_OK on success, B_NO_MEMORY if memory could not be 34 allocated for processing, B_BAD_TYPE if the deflate data is 35 invalid or incomplete. 36 */ 37 38 #define CHUNK (16 * 1024) 39 40 static status_t 41 inflater(int fd, char *dest, int dest_size) 42 { 43 char in[CHUNK]; 44 z_stream strm; 45 int ret, shift = 0; 46 47 // allocate inflate state 48 strm.zalloc = Z_NULL; 49 strm.zfree = Z_NULL; 50 strm.opaque = Z_NULL; 51 strm.avail_in = 0; 52 strm.next_in = Z_NULL; 53 ret = inflateInit(&strm); 54 // decompress until end of file 55 do { 56 strm.avail_in = read(fd, in, CHUNK); 57 if (strm.avail_in == 0) { 58 (void)inflateEnd(&strm); 59 return B_BAD_TYPE; 60 } 61 if (shift == 0 && memcmp(in + 1,"ELF",3) == 0) { 62 memcpy((Bytef *) dest,in,strm.avail_in); 63 if (strm.avail_in == CHUNK) { 64 read(fd, dest + strm.avail_in, dest_size - CHUNK); 65 } 66 (void)inflateEnd(&strm); 67 return B_OK; 68 } 69 strm.next_in = (Bytef *) in; 70 strm.avail_out = dest_size - shift; 71 strm.next_out = (Bytef *) dest + shift; 72 ret = inflate(&strm, Z_NO_FLUSH); 73 switch (ret) { 74 case Z_NEED_DICT: 75 case Z_DATA_ERROR: 76 (void)inflateEnd(&strm); 77 return B_BAD_TYPE; 78 case Z_MEM_ERROR: 79 (void)inflateEnd(&strm); 80 return B_NO_MEMORY; 81 } 82 shift = dest_size - strm.avail_out; 83 if (strm.avail_out == 0) { 84 (void)inflateEnd(&strm); 85 return B_NO_MEMORY; 86 } 87 } while (ret != Z_STREAM_END); 88 (void)inflateEnd(&strm); 89 return B_OK; 90 } 91 92 31 93 void 32 94 elf_init() 33 95 { … … 119 181 120 182 121 183 static status_t 122 load_elf_symbol_table(int fd, preloaded_image *image )184 load_elf_symbol_table(int fd, preloaded_image *image, char *unzip_buf) 123 185 { 124 186 struct Elf32_Ehdr &elfHeader = image->elf_header; 125 187 Elf32_Sym *symbolTable = NULL; … … 137 199 return B_NO_MEMORY; 138 200 } 139 201 140 ssize_t length = read_pos(fd, elfHeader.e_shoff, sectionHeaders, size); 141 if (length < size) { 142 TRACE(("error reading in program headers\n")); 143 status = B_ERROR; 144 goto error1; 145 } 202 memcpy(sectionHeaders, unzip_buf+elfHeader.e_shoff, size); 146 203 147 204 // find symbol table in section headers 148 205 … … 164 221 goto error1; 165 222 } 166 223 167 length = read_pos(fd, sectionHeaders[i].sh_offset, symbolTable, 168 size); 169 if (length < size) { 170 TRACE(("error reading in symbol table\n")); 171 status = B_ERROR; 172 goto error1; 173 } 224 memcpy(symbolTable, unzip_buf + sectionHeaders[i].sh_offset, size); 174 225 175 226 numSymbols = size / sizeof(Elf32_Sym); 176 227 break; … … 191 242 goto error2; 192 243 } 193 244 194 length = read_pos(fd, stringHeader->sh_offset, stringTable, size); 195 if (length < size) { 196 TRACE(("error reading in string table\n")); 197 status = B_ERROR; 198 goto error3; 199 } 245 memcpy(stringTable, unzip_buf + stringHeader->sh_offset, size); 200 246 201 247 TRACE(("loaded %ld debug symbols\n", numSymbols)); 202 248 … … 209 255 free(sectionHeaders); 210 256 return B_OK; 211 257 212 error3:213 kernel_args_free(stringTable);214 258 error2: 215 259 kernel_args_free(symbolTable); 216 260 error1: … … 221 265 222 266 223 267 status_t 224 elf_load_image(int fd, preloaded_image *image )268 elf_load_image(int fd, preloaded_image *image, char *unzip_buf, int unzip_buf_size) 225 269 { 226 270 size_t totalSize; 227 271 status_t status; 228 272 229 273 TRACE(("elf_load_image(fd = %d, image = %p)\n", fd, image)); 230 274 275 inflater(fd, unzip_buf, unzip_buf_size ); 276 231 277 struct Elf32_Ehdr &elfHeader = image->elf_header; 232 233 ssize_t length = read_pos(fd, 0, &elfHeader, sizeof(Elf32_Ehdr)); 234 if (length < (ssize_t)sizeof(Elf32_Ehdr)) 235 return B_BAD_TYPE; 236 278 memcpy(&elfHeader, unzip_buf, sizeof(Elf32_Ehdr)); 237 279 status = verify_elf_header(elfHeader); 238 280 if (status < B_OK) 239 281 return status; … … 245 287 return B_NO_MEMORY; 246 288 } 247 289 248 length = read_pos(fd, elfHeader.e_phoff, programHeaders, size); 249 if (length < size) { 250 TRACE(("error reading in program headers\n")); 251 status = B_ERROR; 252 goto error1; 253 } 290 memcpy(programHeaders, unzip_buf + elfHeader.e_phoff, size); 254 291 255 // create an area large enough to hold the image256 257 292 image->data_region.size = 0; 258 293 image->text_region.size = 0; 259 294 … … 361 396 362 397 TRACE(("load segment %d (%ld bytes)...\n", i, header.p_filesz)); 363 398 364 length = read_pos(fd, header.p_offset, 365 (void *)(region->start + (header.p_vaddr % B_PAGE_SIZE)), 366 header.p_filesz); 367 if (length < (ssize_t)header.p_filesz) { 368 status = B_BAD_DATA; 369 dprintf("error reading in seg %d\n", i); 370 goto error2; 371 } 399 memcpy((void *)(region->start + (header.p_vaddr % B_PAGE_SIZE)), 400 unzip_buf + header.p_offset, 401 header.p_filesz); 372 402 373 403 // Clear anything above the file size (that may also contain the BSS 374 404 // area) … … 388 418 image->debug_string_table = NULL; 389 419 390 420 if (sLoadElfSymbols) 391 load_elf_symbol_table(fd, image );421 load_elf_symbol_table(fd, image, unzip_buf); 392 422 393 423 free(programHeaders); 394 424 395 425 return B_OK; 396 426 397 error2:398 if (image->text_region.start != 0)399 platform_free_region((void *)image->text_region.start, totalSize);400 427 error1: 401 428 free(programHeaders); 402 429 … … 405 432 406 433 407 434 status_t 408 elf_load_image(Directory *directory, const char *path )435 elf_load_image(Directory *directory, const char *path, char *unzip_buf, int unzip_buf_size) 409 436 { 410 437 preloaded_image *image; 411 438 … … 437 464 return B_NO_MEMORY; 438 465 } 439 466 440 status_t status = elf_load_image(fd, image );467 status_t status = elf_load_image(fd, image, unzip_buf, unzip_buf_size); 441 468 if (status == B_OK) { 442 469 image->name = kernel_args_strdup(path); 443 470 image->inode = stat.st_ino; -
src/system/boot/loader/loader.cpp
52 52 53 53 54 54 status_t 55 load_kernel(stage2_args *args, Directory *volume )55 load_kernel(stage2_args *args, Directory *volume, char *unzip_buf, int unzip_buf_size) 56 56 { 57 57 int fd = open_from(volume, KERNEL_PATH, O_RDONLY); 58 58 if (fd < B_OK) … … 61 61 dprintf("load kernel...\n"); 62 62 63 63 elf_init(); 64 status_t status = elf_load_image(fd, &gKernelArgs.kernel_image );64 status_t status = elf_load_image(fd, &gKernelArgs.kernel_image, unzip_buf, unzip_buf_size); 65 65 66 66 close(fd); 67 67 … … 83 83 84 84 85 85 static status_t 86 load_modules_from(Directory *volume, const char *path )86 load_modules_from(Directory *volume, const char *path, char *unzip_buf, int unzip_buf_size) 87 87 { 88 88 // we don't have readdir() & co. (yet?)... 89 89 … … 102 102 if (!strcmp(name, ".") || !strcmp(name, "..")) 103 103 continue; 104 104 105 status_t status = elf_load_image(modules, name );105 status_t status = elf_load_image(modules, name, unzip_buf, unzip_buf_size); 106 106 if (status != B_OK) 107 107 dprintf("Could not load \"%s\" error %ld\n", name, status); 108 108 } … … 121 121 */ 122 122 123 123 static status_t 124 load_module(Directory *volume, const char *name )124 load_module(Directory *volume, const char *name, char *unzip_buf, int unzip_buf_size) 125 125 { 126 126 char moduleName[B_FILE_NAME_LENGTH]; 127 127 if (strlcpy(moduleName, name, sizeof(moduleName)) > sizeof(moduleName)) … … 142 142 if (fstat(fd, &stat) != 0 || !S_ISREG(stat.st_mode)) 143 143 return B_BAD_VALUE; 144 144 145 status_t status = elf_load_image(base, moduleName );145 status_t status = elf_load_image(base, moduleName, unzip_buf, unzip_buf_size); 146 146 147 147 close(fd); 148 148 close(baseFD); … … 166 166 167 167 168 168 status_t 169 load_modules(stage2_args *args, Directory *volume )169 load_modules(stage2_args *args, Directory *volume, char *unzip_buf, int unzip_buf_size) 170 170 { 171 171 int32 failed = 0; 172 172 … … 176 176 char path[B_FILE_NAME_LENGTH]; 177 177 sprintf(path, "%s/boot", sPaths[i]); 178 178 179 if (load_modules_from(volume, path ) != B_OK)179 if (load_modules_from(volume, path, unzip_buf, unzip_buf_size) != B_OK) 180 180 failed++; 181 181 } 182 182 … … 189 189 for (int32 i = 0; paths[i]; i++) { 190 190 char path[B_FILE_NAME_LENGTH]; 191 191 sprintf(path, "%s/%s", sPaths[0], paths[i]); 192 load_modules_from(volume, path );192 load_modules_from(volume, path, unzip_buf, unzip_buf_size); 193 193 } 194 194 } 195 195 … … 202 202 Partition *partition; 203 203 if (gRoot->GetPartitionFor(volume, &partition) == B_OK) { 204 204 while (partition != NULL) { 205 load_module(volume, partition->ModuleName() );205 load_module(volume, partition->ModuleName(), unzip_buf, unzip_buf_size); 206 206 partition = partition->Parent(); 207 207 } 208 208 } … … 214 214 // loading approach... 215 215 char path[B_FILE_NAME_LENGTH]; 216 216 sprintf(path, "%s/%s", sPaths[0], "file_systems"); 217 load_modules_from(volume, path );217 load_modules_from(volume, path, unzip_buf, unzip_buf_size); 218 218 } 219 219 220 220 return B_OK; -
src/system/boot/loader/elf.h
11 11 12 12 13 13 extern void elf_init(); 14 extern status_t elf_load_image(Directory *directory, const char *path );15 extern status_t elf_load_image(int fd, preloaded_image *image );14 extern status_t elf_load_image(Directory *directory, const char *path, char *unzip_buf, int unzip_buf_size); 15 extern status_t elf_load_image(int fd, preloaded_image *image, char *unzip_buf, int unzip_buf_size); 16 16 17 17 extern status_t elf_relocate_image(struct preloaded_image *image); 18 18 -
src/system/boot/loader/loader.h
10 10 11 11 12 12 extern bool is_bootable(Directory *volume); 13 extern status_t load_kernel(stage2_args *args, Directory *volume );14 extern status_t load_modules(stage2_args *args, Directory *volume );13 extern status_t load_kernel(stage2_args *args, Directory *volume, char *unzip_buf, int unzip_buf_size); 14 extern status_t load_modules(stage2_args *args, Directory *volume, char *unzip_buf, int unzip_buf_size); 15 15 16 16 #endif /* LOADER_H */ -
src/system/boot/loader/main.cpp
24 24 # define TRACE(x) ; 25 25 #endif 26 26 27 #define KERNEL_BUF_SIZE (4*1024*1024) 28 #define UNZIP_BUF_SIZE (4*1024*1024) 27 29 28 30 extern "C" int 29 31 main(stage2_args *args) … … 76 78 77 79 if (volume != NULL) { 78 80 // we got a volume to boot from! 81 82 // allocate upper memory for uncompress buffer 83 char *kernel_buf = (char *)kernel_args_malloc(KERNEL_BUF_SIZE); 84 char *unzip_buf = (char *)kernel_args_malloc(UNZIP_BUF_SIZE); 85 kernel_args_free(kernel_buf); 86 if (!unzip_buf) { 87 panic("Could not allocate memory for uncompressing kernel!\n" ); 88 } 89 79 90 status_t status; 80 while ((status = load_kernel(args, volume )) < B_OK) {91 while ((status = load_kernel(args, volume, unzip_buf, UNZIP_BUF_SIZE)) < B_OK) { 81 92 // loading the kernel failed, so let the user choose another 82 93 // volume to boot from until it works 83 94 volume = NULL; … … 105 116 if ((platform_boot_options() & BOOT_OPTION_DEBUG_OUTPUT) == 0) 106 117 platform_switch_to_logo(); 107 118 108 load_modules(args, volume); 119 load_modules(args, volume, unzip_buf, UNZIP_BUF_SIZE); 120 kernel_args_free(unzip_buf); 109 121 load_driver_settings(args, volume); 110 122 111 123 // set up kernel args version info -
src/system/boot/loader/Jamfile
9 9 UsePrivateHeaders [ FDirName kernel util ] ; 10 10 UsePrivateHeaders shared storage ; 11 11 12 UseHeaders [ FDirName $(HAIKU_TOP) headers libs zlib ] ; 13 SubDirSysHdrs $(HAIKU_TOP) headers libs zlib ; 14 12 15 { 13 16 local defines = 14 17 _BOOT_MODE … … 71 74 list.cpp 72 75 safemode_settings.cpp 73 76 77 # compression 78 crc32.c 79 zutil.c 80 uncompr.c 74 81 : -fno-pic 75 82 ; 76 83 … … 116 123 SEARCH on [ FGristFiles stage2_crt0.S ] 117 124 = [ FDirName $(HAIKU_TOP) src system boot arch $(TARGET_ARCH) ] ; 118 125 119 126 SEARCH on [ FGristFiles crc32.c zutil.c uncompr.c ] 127 = [ FDirName $(HAIKU_TOP) src libs zlib ] ; 128 120 129 SubInclude HAIKU_TOP src system boot loader file_systems ; 121 130 SubInclude HAIKU_TOP src system boot loader net ; -
src/bin/Jamfile
248 248 SubInclude HAIKU_TOP src bin gzip ; 249 249 SubInclude HAIKU_TOP src bin bzip2 ; 250 250 SubInclude HAIKU_TOP src bin tar ; 251 SubInclude HAIKU_TOP src bin recompress ; -
src/bin/recompress/recompress.c
1 /*! Simple compress/uncompress. Use ZLIB */ 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <zlib.h> 5 6 #define MAX_FILE_SIZE (4*1024*1024) 7 8 int 9 main(int argc, char **argv) 10 { 11 FILE *file; 12 char *fdata; 13 char *ndata; 14 unsigned long fsize, nsize; 15 int compression_level = 6; 16 17 if (argc > 4 || argc < 3) { 18 fprintf(stderr, "Compress/Uncompress file.\n"); 19 fprintf(stderr, "Usage: recompress src_file dst_file [-123456789]\n"); 20 fprintf(stderr, "-1 low compression\n"); 21 fprintf(stderr, "-9 high compression\n"); 22 fprintf(stderr, "-6 default compression\n"); 23 return 1; 24 } 25 26 if (argc == 4 && argv[3][0] == '-' 27 && argv[3][1] > '0' && argv[3][1] <= '9' && argv[3][2] == 0) { 28 compression_level = argv[3][1] - '0'; 29 } 30 31 file = fopen(argv[1], "rb"); 32 if (file == 0) { 33 fprintf(stderr, "File not found: %s\n", argv[1]); 34 return 2; 35 } 36 37 fseek(file, 0, SEEK_END); 38 fsize = ftell(file); 39 fseek(file, 0, SEEK_SET); 40 41 fdata = malloc(fsize); 42 ndata = malloc(10 * fsize); 43 44 fread(fdata, 1, MAX_FILE_SIZE, file); 45 fclose(file); 46 47 nsize = MAX_FILE_SIZE; 48 if (uncompress((Bytef *) ndata, &nsize, (Bytef *) fdata, fsize)) { 49 // Compress uncompressed file 50 nsize = MAX_FILE_SIZE; 51 compress2((Bytef *) ndata, &nsize, (Bytef *) fdata, fsize, 52 compression_level); 53 printf("Compressing %s to %s", argv[1], argv[2]); 54 } else { 55 printf("Uncompressing %s to %s", argv[1], argv[2]); 56 } 57 printf(" -> %ld%% Old_size=%ld New_size=%ld\n", 58 (200 * nsize + 1) / (2 * fsize), fsize, nsize); 59 file = fopen(argv[2], "wb"); 60 if (file == 0) { 61 printf("Can't create file: %s\n", argv[2]); 62 return 3; 63 } 64 fwrite(ndata, nsize, 1, file); 65 fclose(file); 66 free(fdata); 67 free(ndata); 68 return 0; 69 } 70 -
src/bin/recompress/Jamfile
1 SubDir HAIKU_TOP src bin recompress ; 2 3 SetSubDirSupportedPlatformsBeOSCompatible ; 4 5 Objects $(common_files) $(common_files2) $(util_files) ; 6 7 UseLibraryHeaders zlib ; 8 SEARCH_SOURCE += [ FDirName $(HAIKU_TOP) src libs zlib ] ; 9 10 SubDirCcFlags -DNOID -DHAVE_STDINT_H=0 ; 11 12 SubDirSysHdrs [ FDirName $(SUBDIR) include ] ; 13 SubDirSysHdrs [ FDirName $(SUBDIR) lib ] ; 14 15 Depends recompress : recompres ; 16 17 BinCommand recompress : 18 recompress.c 19 compress.c 20 uncompr.c 21 deflate.c 22 crc32.c 23 adler32.c 24 inflate.c 25 zutil.c 26 trees.c 27 inffast.c 28 inftrees.c 29 : be 30 ; 31 32 BuildPlatformMain recompres : 33 recompress.c 34 compress.c 35 uncompr.c 36 deflate.c 37 crc32.c 38 adler32.c 39 inflate.c 40 zutil.c 41 trees.c 42 inffast.c 43 inftrees.c 44 ; 45 46