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