Search code examples
cbinarykernelfwritehexdump

how to write data from kernel into binary file


I used an API to get data from kernel which API provides part info as follows.

#API INPUT:
#      size = 0x100 (User/driver will need to allocate buf of size*4,
             #                     since driver dump data in %02x format)

# API OUTPUT:
             #      buf = data copied to this buffer
             #      size = 0x100 * 4
             #      rv = return value.      None zero indicates error

Then i want to save these data from kernel into a binary file.

char *buffer = malloc(0x100 *4);
FILE *fd;
fd = fopen("binfile", "wb");
...
fwrite(buffer, 1, 0x100 *4, fd);
...

But i found the content in this binfile are text. I can use 'cat binfile' to see the content and output as follows.

03 04 07 10 00 00 00 00 00 00 00 06 67 00 00 00 08 03 00 1e 46 49 4e 49 53 41 52 20 43 4f 52 50 2e 20 20 20 00 00 90 65 46 54 4c 58 38 35 37 31 44 33 42 4e 4c 2d 45 35 42 20 20 20 03 52 00 9b 00 1a 00 00 41 4d 51 30 43 36 31 20 20 20 20 20 20 20 20 20 31 32 30 36 31 34 20 20 68 f0 03 bc 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

And use 'hexdump binfile', output is as follows.

0000000 3330 3020 2034 3730 3120 2030 3030 3020
0000010 2030 3030 3020 2030 3030 3020 2030 3030
0000020 3020 2036 3736 3020 2030 3030 3020 2030
0000030 3830 3020 2033 3030 3120 2065 3634 3420
0000040 2039 6534 3420 2039 3335 3420 2031 3235
0000050 3220 2030 3334 3420 2066 3235 3520 2030
0000060 6532 3220 2030 3032 3220 2030 3030 3020
0000070 2030 3039 3620 2035 3634 3520 2034 6334
0000080 3520 2038 3833 3320 2035 3733 3320 2031
0000090 3434 3320 2033 3234 3420 2065 6334 3220
00000a0 2064 3534 3320 2035 3234 3220 2030 3032
00000b0 3220 2030 3330 3520 2032 3030 3920 2062
00000c0 3030 3120 2061 3030 3020 2030 3134 3420
00000d0 2064 3135 3320 2030 3334 3320 2036 3133
00000e0 3220 2030 3032 3220 2030 3032 3220 2030
00000f0 3032 3220 2030 3032 3220 2030 3133 3320
...
00002c0 3020 2030 3030 3020 2030 3030 3020 2030
00002d0 3030 3020 2030 3030 3020 2030 3030 3020
00002e0 2030 3030 3020 2030 3030 3020 2030 3030
00002f0 3020 2030 3030 3020 2030 3030 3020 2030
0000300 0000 0000 0000 0000 0000 0000 0000 0000
*
0000400 >>> it is 0x400

But my expected content of binary file should be as follows.

0000000 0403 0007 0000 4000 0440 0670 0a8c 0000
0000010 0003 0a00 4946 494e 4153 2052 4f43 5052
0000020 202e 2020 0000 6590 5446 464c 3538 3932
0000030 3350 4e42 2d56 3545 3141 2020 5203 a900
0000040 3a00 0000 5155 3036 3030 2048 2020 2020
0000050 2020 2020 3331 3830 3430 2020 fa68 e505
0000060 0000 0000 0000 0000 0000 0000 0000 0000
*
0000100 >>> only 0x100

Solution

  • Note the following from the comment on your API:

    (User/driver will need to allocate buf of size*4, since driver dump data in %02x format)

    You're not accidentally converting it to text; that's how the API gives it to you. You need to convert it to binary yourself. Once you do that, you can just write it the way you are now and it will stay binary.