1/*
2 *  kernelcache.h
3 *  kext_tools
4 *
5 *  Created by Nik Gervae on 2010 10 04.
6 *  Copyright 2010, 2012 Apple Computer, Inc. All rights reserved.
7 *
8 */
9#ifndef _KERNELCACHE_H_
10#define _KERNELCACHE_H_
11
12#include <libc.h>
13#include "kext_tools_util.h"
14
15#define PLATFORM_NAME_LEN  (64)
16#define ROOT_PATH_LEN     (256)
17
18#define COMP_TYPE_LZSS      'lzss'
19#define COMP_TYPE_FASTLIB   'lzvn'
20
21
22// prelinkVersion value >= 1 means KASLR supported
23typedef struct prelinked_kernel_header {
24    uint32_t  signature;
25    uint32_t  compressType;
26    uint32_t  adler32;
27    uint32_t  uncompressedSize;
28    uint32_t  compressedSize;
29    uint32_t  prelinkVersion;
30    uint32_t  reserved[10];
31    char      platformName[PLATFORM_NAME_LEN]; // unused
32    char      rootPath[ROOT_PATH_LEN];         // unused
33    char      data[0];
34} PrelinkedKernelHeader;
35
36typedef struct platform_info {
37    char platformName[PLATFORM_NAME_LEN];
38    char rootPath[ROOT_PATH_LEN];
39} PlatformInfo;
40
41/*******************************************************************************
42*******************************************************************************/
43
44ExitStatus
45writeFatFile(
46    const char                * filePath,
47    CFArrayRef                  fileSlices,
48    CFArrayRef                  fileArchs,
49    mode_t                      fileMode,
50    const struct timeval        fileTimes[2]);
51void * mapAndSwapFatHeaderPage(
52    int fileDescriptor);
53void unmapFatHeaderPage(
54    void *headerPage);
55struct fat_arch * getFirstFatArch(
56    u_char *headerPage);
57struct fat_arch * getNextFatArch(
58    u_char *headerPage,
59    struct fat_arch *lastArch);
60struct fat_arch * getFatArchForArchInfo(
61    u_char *headerPage,
62    const NXArchInfo *archInfo);
63const NXArchInfo *
64getThinHeaderPageArch(
65    const void *headerPage);
66ExitStatus readFatFileArchsWithPath(
67    const char        * filePath,
68    CFMutableArrayRef * archsOut);
69ExitStatus readFatFileArchsWithHeader(
70    u_char            * headerPage,
71    CFMutableArrayRef * archsOut);
72ExitStatus readMachOSlices(
73    const char        * filePath,
74    CFMutableArrayRef * slicesOut,
75    CFMutableArrayRef * archsOut,
76    mode_t            * modeOut,
77    struct timeval      machOTimesOut[2]);
78
79CF_RETURNS_RETAINED
80CFDataRef  readMachOSliceForArch(
81    const char        * filePath,
82    const NXArchInfo  * archInfo,
83    Boolean             checkArch);
84
85CF_RETURNS_RETAINED
86CFDataRef readMachOSlice(
87    int         fileDescriptor,
88    off_t       fileOffset,
89    size_t      fileSliceSize);
90int readFileAtOffset(
91    int             fileDescriptor,
92    off_t           fileOffset,
93    size_t          fileSize,
94    u_char        * buf);
95int verifyMachOIsArch(
96    const UInt8      * fileBuf,
97    size_t              size,
98    const NXArchInfo * archInfo);
99Boolean supportsFastLibCompression(
100    void);
101CF_RETURNS_RETAINED
102CFDataRef uncompressPrelinkedSlice(
103    CFDataRef prelinkImage);
104CF_RETURNS_RETAINED
105CFDataRef compressPrelinkedSlice(
106    uint32_t            compressionType,
107    CFDataRef           prelinkImage,
108    Boolean             hasRelocs);
109ExitStatus writePrelinkedSymbols(
110    CFURLRef    symbolDirURL,
111    CFArrayRef  prelinkSymbols,
112    CFArrayRef  prelinkArchs);
113ExitStatus makeDirectoryWithURL(
114    CFURLRef dirURL);
115
116#endif /* _KERNELCACHE_H_ */
117