xref: /aosp_15_r20/external/mesa3d/src/freedreno/decode/rdutil.h (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2022 Igalia S.L.
3  * SPDX-License-Identifier: MIT
4  */
5 
6 #include <stdint.h>
7 #include <stdio.h>
8 #include <string.h>
9 
10 #include "io.h"
11 #include "redump.h"
12 
13 static void
parse_addr(uint32_t * buf,int sz,unsigned int * len,uint64_t * gpuaddr)14 parse_addr(uint32_t *buf, int sz, unsigned int *len, uint64_t *gpuaddr)
15 {
16    *gpuaddr = buf[0];
17    *len = buf[1];
18    if (sz > 8)
19       *gpuaddr |= ((uint64_t)(buf[2])) << 32;
20 }
21 
22 static uint32_t
parse_gpu_id(void * buf)23 parse_gpu_id(void *buf)
24 {
25    return *((unsigned int *)buf);
26 }
27 
28 static uint64_t
parse_chip_id(void * buf)29 parse_chip_id(void *buf)
30 {
31    return *((uint64_t *)buf);
32 }
33 
34 struct rd_parsed_section
35 {
36    void *buf;
37    enum rd_sect_type type;
38    int sz;
39    int ret;
40 };
41 
42 static bool
parse_rd_section(struct io * io,struct rd_parsed_section * section)43 parse_rd_section(struct io *io, struct rd_parsed_section *section)
44 {
45    uint32_t arr[2];
46    int ret;
47 
48    ret = io_readn(io, arr, 8);
49    if (ret <= 0)
50       goto end;
51 
52    while ((arr[0] == 0xffffffff) && (arr[1] == 0xffffffff)) {
53       ret = io_readn(io, arr, 8);
54       if (ret <= 0)
55          goto end;
56    }
57 
58    section->type = arr[0];
59    section->sz = arr[1];
60 
61    if (section->sz < 0) {
62       ret = -1;
63       goto end;
64    }
65 
66    free(section->buf);
67 
68    section->buf = malloc(section->sz + 1);
69    ((char *)section->buf)[section->sz] = '\0';
70    ret = io_readn(io, section->buf, section->sz);
71    if (ret < 0)
72       goto end;
73 
74    section->ret = ret;
75    return true;
76 
77 end:
78    section->ret = ret;
79    return false;
80 }
81