| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <errno.h> | 5 #include <errno.h> |
| 6 #include <fcntl.h> | 6 #include <fcntl.h> |
| 7 #include <limits.h> | 7 #include <limits.h> |
| 8 #include <lzma.h> |
| 8 #include <stdio.h> | 9 #include <stdio.h> |
| 9 #include <string.h> | 10 #include <string.h> |
| 10 #include <sys/mman.h> | 11 #include <sys/mman.h> |
| 11 #include <sys/stat.h> | 12 #include <sys/stat.h> |
| 12 #include <sys/types.h> | 13 #include <sys/types.h> |
| 13 #include <unistd.h> | 14 #include <unistd.h> |
| 14 | 15 |
| 15 #include "bmpblk_util.h" | 16 #include "bmpblk_util.h" |
| 16 #include "eficompress.h" | 17 #include "eficompress.h" |
| 17 | 18 |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 | 93 |
| 93 static void *do_efi_decompress(ImageInfo *img) { | 94 static void *do_efi_decompress(ImageInfo *img) { |
| 94 void *ibuf; | 95 void *ibuf; |
| 95 void *sbuf; | 96 void *sbuf; |
| 96 void *obuf; | 97 void *obuf; |
| 97 uint32_t isize; | 98 uint32_t isize; |
| 98 uint32_t ssize; | 99 uint32_t ssize; |
| 99 uint32_t osize; | 100 uint32_t osize; |
| 100 EFI_STATUS r; | 101 EFI_STATUS r; |
| 101 | 102 |
| 102 ibuf = ((void *)img) + sizeof(ImageInfo); | 103 ibuf = (void*)(img + 1); |
| 103 isize = img->compressed_size; | 104 isize = img->compressed_size; |
| 104 | 105 |
| 105 r = EfiGetInfo(ibuf, isize, &osize, &ssize); | 106 r = EfiGetInfo(ibuf, isize, &osize, &ssize); |
| 106 if (EFI_SUCCESS != r) { | 107 if (EFI_SUCCESS != r) { |
| 107 fprintf(stderr, "EfiGetInfo() failed with code %d\n", | 108 fprintf(stderr, "EfiGetInfo() failed with code %d\n", |
| 108 r); | 109 r); |
| 109 return 0; | 110 return 0; |
| 110 } | 111 } |
| 111 | 112 |
| 112 sbuf = malloc(ssize); | 113 sbuf = malloc(ssize); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 132 free(obuf); | 133 free(obuf); |
| 133 free(sbuf); | 134 free(sbuf); |
| 134 return 0; | 135 return 0; |
| 135 } | 136 } |
| 136 | 137 |
| 137 free(sbuf); | 138 free(sbuf); |
| 138 return obuf; | 139 return obuf; |
| 139 } | 140 } |
| 140 | 141 |
| 141 | 142 |
| 143 |
| 144 static void *do_lzma_decompress(ImageInfo *img) { |
| 145 void *ibuf; |
| 146 void *obuf; |
| 147 uint32_t isize; |
| 148 uint32_t osize; |
| 149 lzma_stream stream = LZMA_STREAM_INIT; |
| 150 lzma_ret result; |
| 151 |
| 152 ibuf = (void*)(img + 1); |
| 153 isize = img->compressed_size; |
| 154 osize = img->original_size; |
| 155 obuf = malloc(osize); |
| 156 if (!obuf) { |
| 157 fprintf(stderr, "Can't allocate %d bytes: %s\n", |
| 158 osize, |
| 159 strerror(errno)); |
| 160 return 0; |
| 161 } |
| 162 |
| 163 result = lzma_auto_decoder(&stream, -1, 0); |
| 164 if (result != LZMA_OK) { |
| 165 fprintf(stderr, "Unable to initialize auto decoder (error: %d)!\n", |
| 166 result); |
| 167 free(obuf); |
| 168 return 0; |
| 169 } |
| 170 |
| 171 stream.next_in = ibuf; |
| 172 stream.avail_in = isize; |
| 173 stream.next_out = obuf; |
| 174 stream.avail_out = osize; |
| 175 result = lzma_code(&stream, LZMA_FINISH); |
| 176 if (result != LZMA_STREAM_END) { |
| 177 fprintf(stderr, "Unalbe to decode data (error: %d)!\n", result); |
| 178 free(obuf); |
| 179 return 0; |
| 180 } |
| 181 lzma_end(&stream); |
| 182 return obuf; |
| 183 } |
| 184 |
| 185 |
| 186 |
| 142 // Show what's inside. If todir is NULL, just print. Otherwise unpack. | 187 // Show what's inside. If todir is NULL, just print. Otherwise unpack. |
| 143 int dump_bmpblock(const char *infile, int show_as_yaml, | 188 int dump_bmpblock(const char *infile, int show_as_yaml, |
| 144 const char *todir, int overwrite) { | 189 const char *todir, int overwrite) { |
| 145 void *ptr, *data_ptr; | 190 void *ptr, *data_ptr; |
| 146 size_t length = 0; | 191 size_t length = 0; |
| 147 BmpBlockHeader *hdr; | 192 BmpBlockHeader *hdr; |
| 148 ImageInfo *img; | 193 ImageInfo *img; |
| 149 ScreenLayout *scr; | 194 ScreenLayout *scr; |
| 150 int loc_num; | 195 int loc_num; |
| 151 int screen_num; | 196 int screen_num; |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 259 case COMPRESS_EFIv1: | 304 case COMPRESS_EFIv1: |
| 260 data_ptr = do_efi_decompress(img); | 305 data_ptr = do_efi_decompress(img); |
| 261 if (!data_ptr) { | 306 if (!data_ptr) { |
| 262 fclose(bfp); | 307 fclose(bfp); |
| 263 fclose(yfp); | 308 fclose(yfp); |
| 264 discard_file(ptr, length); | 309 discard_file(ptr, length); |
| 265 return 1; | 310 return 1; |
| 266 } | 311 } |
| 267 free_data = 1; | 312 free_data = 1; |
| 268 break; | 313 break; |
| 314 case COMPRESS_LZMA1: |
| 315 data_ptr = do_lzma_decompress(img); |
| 316 if (!data_ptr) { |
| 317 fclose(bfp); |
| 318 fclose(yfp); |
| 319 discard_file(ptr, length); |
| 320 return 1; |
| 321 } |
| 322 free_data = 1; |
| 323 break; |
| 269 default: | 324 default: |
| 270 fprintf(stderr, "Unsupported compression method encountered.\n"); | 325 fprintf(stderr, "Unsupported compression method encountered.\n"); |
| 271 fclose(bfp); | 326 fclose(bfp); |
| 272 fclose(yfp); | 327 fclose(yfp); |
| 273 discard_file(ptr, length); | 328 discard_file(ptr, length); |
| 274 return 1; | 329 return 1; |
| 275 } | 330 } |
| 276 if (1 != fwrite(data_ptr, img->original_size, 1, bfp)) { | 331 if (1 != fwrite(data_ptr, img->original_size, 1, bfp)) { |
| 277 fprintf(stderr, "Unable to write %s: %s\n", full_path_name, | 332 fprintf(stderr, "Unable to write %s: %s\n", full_path_name, |
| 278 strerror(errno)); | 333 strerror(errno)); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 327 } | 382 } |
| 328 | 383 |
| 329 if (todir) | 384 if (todir) |
| 330 fclose(yfp); | 385 fclose(yfp); |
| 331 | 386 |
| 332 discard_file(ptr, length); | 387 discard_file(ptr, length); |
| 333 | 388 |
| 334 return 0; | 389 return 0; |
| 335 } | 390 } |
| 336 | 391 |
| OLD | NEW |