1 /*
2  * Copyright 2014 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 <dumpstate.h>
18 #include <errno.h>
19 #include <fcntl.h>
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23 
24 static const char base64[] =
25     "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
26 static const char pad64 = '=';
27 
base64_output3(const unsigned char * src,int len)28 static void base64_output3(const unsigned char *src, int len)
29 {
30     printf("%c", base64[src[0] >> 2]);
31     printf("%c", base64[((src[0] & 0x03) << 4) | (src[1] >> 4)]);
32     if (len == 1) {
33         printf("==");
34         return;
35     }
36     printf("%c", base64[((src[1] & 0x0F) << 2) | (src[2] >> 6)]);
37     if (len == 2) {
38         printf("=");
39         return;
40     }
41     printf("%c", base64[src[2] & 0x3F]);
42 }
43 
fugu_dump_base64(const char * path)44 static void fugu_dump_base64(const char *path)
45 {
46 
47     printf("------ (%s) ------\n", path);
48     int fd = open(path, O_RDONLY | O_CLOEXEC | O_NOFOLLOW);
49     if (fd < 0) {
50         printf("*** %s: %s\n\n", path, strerror(errno));
51         return;
52     }
53 
54     /* buffer size multiple of 3 for ease of use */
55     unsigned char buffer[1200];
56     int left = 0;
57     int count = 0;
58     for (;;) {
59         int ret = read(fd, &buffer[left], sizeof(buffer) - left);
60         if (ret <= 0) {
61             break;
62         }
63         left += ret;
64         int ofs = 0;
65         while (left > 2) {
66             base64_output3(&buffer[ofs], 3);
67             left -= 3;
68             ofs += 3;
69             count += 4;
70             if (count > 72) {
71                 printf("\n");
72                 count = 0;
73             }
74         }
75         if (left) {
76             memmove(buffer, &buffer[ofs], left);
77         }
78     }
79     close(fd);
80 
81     if (!left) {
82         printf("\n------ end ------\n");
83         return;
84     }
85 
86     /* finish padding */
87     count = left;
88     while (count < 3) {
89         buffer[count++] = 0;
90     }
91     base64_output3(buffer, left);
92 
93     printf("\n------ end ------\n");
94 }
95 
dumpstate_board()96 void dumpstate_board()
97 {
98     dump_file("INTERRUPTS", "/proc/interrupts");
99     dump_file("last ipanic_console", "/data/dontpanic/ipanic_console");
100     dump_file("last ipanic_threads", "/data/dontpanic/ipanic_threads");
101     fugu_dump_base64("/dev/snd_atvr_mSBC");
102     fugu_dump_base64("/dev/snd_atvr_pcm");
103 };
104