1 /*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <errno.h>
18 #include <stdio.h>
19 #include <string.h>
20
21 #include "bootloader.h"
22 #include "mtdutils/mtdutils.h"
23
24 #define LOGE(...) fprintf(stderr, "E:" __VA_ARGS__)
25
recover_firmware_update_log()26 void recover_firmware_update_log() {
27 printf("recovering log from before firmware update\n");
28
29 mtd_scan_partitions();
30 const MtdPartition *part = mtd_find_partition_by_name(CACHE_NAME);
31 if (part == NULL) {
32 LOGE("Can't find %s\n", CACHE_NAME);
33 return;
34 }
35
36 MtdReadContext* read = mtd_read_partition(part);
37
38 size_t erase_size;
39 if (mtd_partition_info(part, NULL, &erase_size, NULL) != 0) {
40 LOGE("Error reading block size\n(%s)\n", strerror(errno));
41 mtd_read_close(read);
42 return;
43 }
44
45 char* buffer = malloc(erase_size);
46 if (mtd_read_data(read, buffer, erase_size) != erase_size) {
47 LOGE("Error reading header block\n(%s)\n", strerror(errno));
48 mtd_read_close(read);
49 free(buffer);
50 return;
51 }
52 if (mtd_read_data(read, buffer, erase_size) != erase_size) {
53 LOGE("Error reading log block\n(%s)\n", strerror(errno));
54 mtd_read_close(read);
55 free(buffer);
56 return;
57 }
58 mtd_read_close(read);
59
60 if (memcmp(buffer, LOG_MAGIC, LOG_MAGIC_SIZE) != 0) {
61 printf("no log to recover from cache partition\n");
62 free(buffer);
63 return;
64 }
65
66 size_t log_size = *(size_t *)(buffer + LOG_MAGIC_SIZE);
67
68 printf("\n###\n### START RECOVERED LOG\n###\n\n");
69 fwrite(buffer + sizeof(size_t) + LOG_MAGIC_SIZE, 1, log_size, stdout);
70 printf("\n\n###\n### END RECOVERED LOG\n###\n\n");
71
72 free(buffer);
73 }
74