| 1 | /* $Id: extract.c,v 1.7 2002-12-06 02:55:58 bird Exp $ | 
|---|
| 2 | * | 
|---|
| 3 | * Description:     SymDB entry generator. | 
|---|
| 4 | *                  Builds SymDB entry from one or more symbol files. | 
|---|
| 5 | * | 
|---|
| 6 | * Copyright (c) 2000-2001 knut st. osmundsen (knut.stange.osmundsen@mynd.no) | 
|---|
| 7 | * | 
|---|
| 8 | * Project Odin Software License can be found in LICENSE.TXT | 
|---|
| 9 | * | 
|---|
| 10 | */ | 
|---|
| 11 |  | 
|---|
| 12 | /******************************************************************************* | 
|---|
| 13 | *   Defined Constants And Macros                                               * | 
|---|
| 14 | *******************************************************************************/ | 
|---|
| 15 | /* Disable logging */ | 
|---|
| 16 | #define NOLOGGING 1 | 
|---|
| 17 |  | 
|---|
| 18 | #define fclose(a) DosClose(a) | 
|---|
| 19 | #define SEEK_SET FILE_BEGIN | 
|---|
| 20 | #define SEEK_END FILE_END | 
|---|
| 21 |  | 
|---|
| 22 | #define WORD unsigned short int | 
|---|
| 23 | #define DWORD unsigned long int | 
|---|
| 24 |  | 
|---|
| 25 | #define INCL_BASE | 
|---|
| 26 | #define INCL_DOS | 
|---|
| 27 | #define INCL_NOPMAPI | 
|---|
| 28 | #define INCL_OS2KRNL_LDR | 
|---|
| 29 |  | 
|---|
| 30 | /******************************************************************************* | 
|---|
| 31 | *   Header Files                                                               * | 
|---|
| 32 | *******************************************************************************/ | 
|---|
| 33 | #include <os2.h> | 
|---|
| 34 |  | 
|---|
| 35 | #include <strat2.h> | 
|---|
| 36 | #include <reqpkt.h> | 
|---|
| 37 |  | 
|---|
| 38 | #include "devSegDf.h" | 
|---|
| 39 | #undef  DATA16_INIT | 
|---|
| 40 | #define DATA16_INIT | 
|---|
| 41 | #undef  CODE16_INIT | 
|---|
| 42 | #define CODE16_INIT | 
|---|
| 43 | #include "os2Krnl.h"                    /* must be included before dev1632.h! */ | 
|---|
| 44 | #include "probkrnl.h" | 
|---|
| 45 | #include "dev1632.h" | 
|---|
| 46 | #include "vprntf16.h" | 
|---|
| 47 |  | 
|---|
| 48 |  | 
|---|
| 49 | /******************************************************************************* | 
|---|
| 50 | *   Global Variables                                                           * | 
|---|
| 51 | *******************************************************************************/ | 
|---|
| 52 | /* Result from GetKernelInfo/ReadOS2Krnl. */ | 
|---|
| 53 | extern unsigned char   cObjects; | 
|---|
| 54 | extern POTE            paKrnlOTEs; | 
|---|
| 55 |  | 
|---|
| 56 | /* dummy replacement for SymDB.c */ | 
|---|
| 57 | KRNLDBENTRY   DATA16_INIT    aKrnlSymDB[] = {{0}}; | 
|---|
| 58 |  | 
|---|
| 59 | /******************************************************************************* | 
|---|
| 60 | *   Internal Functions                                                         * | 
|---|
| 61 | *******************************************************************************/ | 
|---|
| 62 | int      processFile(const char *pszFilename); | 
|---|
| 63 |  | 
|---|
| 64 | /******************************************************************************* | 
|---|
| 65 | *   External Functions                                                         * | 
|---|
| 66 | *******************************************************************************/ | 
|---|
| 67 | /* Workers */ | 
|---|
| 68 | extern  int      ProbeSymFile(const char *pszFilename); | 
|---|
| 69 |  | 
|---|
| 70 | /* C-library replacements and additions. */ | 
|---|
| 71 | extern  void     kmemcpy(char *psz1, const char *psz2, int cch); | 
|---|
| 72 | extern  char *   kstrstr(const char *psz1, const char *psz2); | 
|---|
| 73 | extern  int      kstrcmp(const char *psz1, const char *psz2); | 
|---|
| 74 | extern  int      kstrncmp(const char *psz1, const char *psz2, int cch); | 
|---|
| 75 | extern  int      kstrnicmp(const char *psz1, const char *psz2, int cch); | 
|---|
| 76 | extern  int      kstrlen(const char *psz); | 
|---|
| 77 | extern  char *   kstrcpy(char * pszTarget, const char * pszSource); | 
|---|
| 78 | extern  int      kargncpy(char *pszTarget, const char *pszArg, unsigned cchMaxlen); | 
|---|
| 79 |  | 
|---|
| 80 |  | 
|---|
| 81 |  | 
|---|
| 82 | /** | 
|---|
| 83 | * Dumps writes a KRNLDBENTRY struct to stderr for the given .sym-file. | 
|---|
| 84 | * The filesnames are on this format: | 
|---|
| 85 | *    nnnn[n]tm[r].SYM | 
|---|
| 86 | * Where: n - are the build number 4 or 5 digits. | 
|---|
| 87 | *        t - kernel type. R = retail, H = half strict, A = all strict. | 
|---|
| 88 | *        m - UNI or SMP.  U = UNI processor kernel. S = SMP processor kernel. 4 = Warp 4 FP13+ | 
|---|
| 89 | *        r - revision letter. Currently only 'A' is supported. | 
|---|
| 90 | * @returns   NO_ERROR on success. Untracable error code on error. | 
|---|
| 91 | * @param     pszFilename  Pointer to read only filename of the .sym-file. | 
|---|
| 92 | * @status    completely implemented. | 
|---|
| 93 | * @author    knut st. osmundsen (knut.stange.osmundsen@mynd.no) | 
|---|
| 94 | * @remark    Currently only retail kernels are processed. See note below. | 
|---|
| 95 | */ | 
|---|
| 96 | static int processFile(const char *pszFilename) | 
|---|
| 97 | { | 
|---|
| 98 | APIRET   rc; | 
|---|
| 99 | int      cch; | 
|---|
| 100 | int      cchNum; | 
|---|
| 101 | const char *psz = pszFilename + kstrlen(pszFilename); | 
|---|
| 102 |  | 
|---|
| 103 | /* find filename */ | 
|---|
| 104 | cch = 0; | 
|---|
| 105 | while (psz >= pszFilename && *psz != '\\' && *psz != '/' && *psz != ':') | 
|---|
| 106 | psz--, cch++; | 
|---|
| 107 | psz++; | 
|---|
| 108 | cch--; | 
|---|
| 109 |  | 
|---|
| 110 | /* Progress information */ | 
|---|
| 111 | DosWrite(2, (char*)pszFilename, cch, &rc); | 
|---|
| 112 | DosWrite(2, "\r\n", 2, &rc); | 
|---|
| 113 |  | 
|---|
| 114 | /* Filename check */ | 
|---|
| 115 | cchNum = psz[0] > '2' ? 4 : 5;      /* build number length. */ | 
|---|
| 116 | if (cch < 10 || cch > 12 | 
|---|
| 117 | || !(psz[0] >= '0' && psz[0] <= '9') | 
|---|
| 118 | || !(psz[1] >= '0' && psz[1] <= '9') | 
|---|
| 119 | || !(psz[2] >= '0' && psz[2] <= '9') | 
|---|
| 120 | || !(psz[3] >= '0' && psz[3] <= '9') | 
|---|
| 121 | || !(cchNum == 4 || (psz[4] >= '0' && psz[4] <= '9')) | 
|---|
| 122 | || !(psz[cchNum] == 'A' || psz[cchNum] == 'H' || psz[cchNum] == 'R' || psz[cchNum] == 'B') | 
|---|
| 123 | || !(psz[cchNum+1] == 'S' || psz[cchNum+1] == 'U' || psz[cchNum+1] == '4') | 
|---|
| 124 | /*        || !(cch != 12 || psz[cchNum+2] == 'A') */ | 
|---|
| 125 | ) | 
|---|
| 126 | { | 
|---|
| 127 | printf16("invalid filename: %s\n", pszFilename); | 
|---|
| 128 | return 2; | 
|---|
| 129 | } | 
|---|
| 130 |  | 
|---|
| 131 | /* | 
|---|
| 132 | * Probe kernelfile | 
|---|
| 133 | */ | 
|---|
| 134 | rc = ProbeSymFile(pszFilename); | 
|---|
| 135 |  | 
|---|
| 136 |  | 
|---|
| 137 | /* | 
|---|
| 138 | * on success dump a struct for this kernel | 
|---|
| 139 | */ | 
|---|
| 140 | if (rc == 0) | 
|---|
| 141 | { | 
|---|
| 142 | int i; | 
|---|
| 143 |  | 
|---|
| 144 | /** @remark | 
|---|
| 145 | * Currently information for retail kernels are usable, but we'll | 
|---|
| 146 | * generate it for the debug kernels too, but this information | 
|---|
| 147 | * is enclaved within an "#ifdef ALLKERNELS ... #endif". | 
|---|
| 148 | */ | 
|---|
| 149 | if (psz[cchNum] != 'R') | 
|---|
| 150 | printf16("#ifdef ALLKERNELS\n"); | 
|---|
| 151 |  | 
|---|
| 152 | printf16("    { /* %s */\n" | 
|---|
| 153 | "        %.*s, ", | 
|---|
| 154 | psz, | 
|---|
| 155 | cchNum, &psz[0]       /* build number */ | 
|---|
| 156 | ); | 
|---|
| 157 |  | 
|---|
| 158 | switch (psz[cchNum + 1]) | 
|---|
| 159 | { | 
|---|
| 160 | case 'S':   printf16("KF_SMP"); break; | 
|---|
| 161 | case '4':   printf16("KF_UNI | KF_W4"); break; | 
|---|
| 162 | case 'U':   printf16("KF_UNI"); break; | 
|---|
| 163 | } | 
|---|
| 164 | switch (psz[cchNum]) | 
|---|
| 165 | { | 
|---|
| 166 | case 'A':   printf16(" | KF_ALLSTRICT"); break; | 
|---|
| 167 | case 'H':   printf16(" | KF_HALFSTRICT"); break; | 
|---|
| 168 | case 'B':   printf16(" | KF_HALFSTRICT"); break; | 
|---|
| 169 | } | 
|---|
| 170 | if (psz[cchNum + 2] >= 'A' && psz[cchNum + 2] <= 'Z') | 
|---|
| 171 | printf16(" | KF_REV_%c", psz[cchNum + 2]); | 
|---|
| 172 |  | 
|---|
| 173 | printf16(", %d,\n" | 
|---|
| 174 | "        {\n", | 
|---|
| 175 | aImportTab[0].iObject + 1); /* ASSUMES that DOSCODE32 is the last object. */ | 
|---|
| 176 |  | 
|---|
| 177 | for (i = 0; i < NBR_OF_KRNLIMPORTS; i++) | 
|---|
| 178 | { | 
|---|
| 179 | char *psz = aImportTab[i].achName; | 
|---|
| 180 | printf16("            {%-2d, 0x%08lx}, /* %s */\n", | 
|---|
| 181 | aImportTab[i].fFound ? aImportTab[i].iObject : 0, | 
|---|
| 182 | aImportTab[i].fFound ? aImportTab[i].offObject : 0xFFFFFFFFUL, | 
|---|
| 183 | (char *)&aImportTab[i].achName[0] | 
|---|
| 184 | ); | 
|---|
| 185 | } | 
|---|
| 186 | printf16("        }\n" | 
|---|
| 187 | "    },\n"); | 
|---|
| 188 |  | 
|---|
| 189 | /** @remark | 
|---|
| 190 | * Currently information for retail kernels are usable, but we'll | 
|---|
| 191 | * generate it for the debug kernels too, but this information | 
|---|
| 192 | * is enclaved within an "#ifdef ALLKERNELS ... #endif". | 
|---|
| 193 | */ | 
|---|
| 194 | if (psz[cchNum] != 'R') | 
|---|
| 195 | printf16("#endif\n"); | 
|---|
| 196 | } | 
|---|
| 197 | else | 
|---|
| 198 | printf16("ProbeSymFile failed with rc=%d\n", rc); | 
|---|
| 199 |  | 
|---|
| 200 | return rc; | 
|---|
| 201 | } | 
|---|
| 202 |  | 
|---|
| 203 |  | 
|---|
| 204 | /** | 
|---|
| 205 | * Extract program. | 
|---|
| 206 | * | 
|---|
| 207 | * This is some initial trial-and-error for creating an "database" of | 
|---|
| 208 | * kernel entrypoints. | 
|---|
| 209 | * | 
|---|
| 210 | * Output to stderr the structs generated for the passed in *.sym file. | 
|---|
| 211 | * | 
|---|
| 212 | */ | 
|---|
| 213 | int main(int argc, char **argv) | 
|---|
| 214 | { | 
|---|
| 215 | APIRET  rc; | 
|---|
| 216 | const char *  psz; | 
|---|
| 217 | int     i; | 
|---|
| 218 |  | 
|---|
| 219 | /* | 
|---|
| 220 | * Set paKrnlOTEs to point to an zeroed array of OTEs. | 
|---|
| 221 | */ | 
|---|
| 222 | static KRNLINFO DATA16_INIT  KrnlInfo = {0}; | 
|---|
| 223 | paKrnlOTEs = &KrnlInfo.aObjects[0]; | 
|---|
| 224 |  | 
|---|
| 225 | /* | 
|---|
| 226 | * Check name lengths. | 
|---|
| 227 | */ | 
|---|
| 228 | for (i = 0; i < NBR_OF_KRNLIMPORTS; i++) | 
|---|
| 229 | { | 
|---|
| 230 | if (kstrlen(aImportTab[i].achName) != (int)aImportTab[i].cchName) | 
|---|
| 231 | { | 
|---|
| 232 | printf16("internal error - bad length of entry %d - %s. %d should be %d.\n", | 
|---|
| 233 | i, aImportTab[i].achName, aImportTab[i].cchName, kstrlen(aImportTab[i].achName)); | 
|---|
| 234 | return -1; | 
|---|
| 235 | } | 
|---|
| 236 | } | 
|---|
| 237 |  | 
|---|
| 238 | /* | 
|---|
| 239 | * Do work. | 
|---|
| 240 | */ | 
|---|
| 241 | if (argc > 1) | 
|---|
| 242 | { | 
|---|
| 243 | /* | 
|---|
| 244 | * Arguments: extract.exe <symfiles...> | 
|---|
| 245 | */ | 
|---|
| 246 | int i; | 
|---|
| 247 | for (i = 1; i < argc; i++) | 
|---|
| 248 | { | 
|---|
| 249 | rc = processFile(argv[i]); | 
|---|
| 250 | if (rc != NO_ERROR) | 
|---|
| 251 | { | 
|---|
| 252 | printf16("processFile failed with rc=%d for file %s\n", | 
|---|
| 253 | rc, argv[i]); | 
|---|
| 254 | if (psz = GetErrorMsg(rc)) | 
|---|
| 255 | printf16("%s\n", psz); | 
|---|
| 256 | return rc; | 
|---|
| 257 | } | 
|---|
| 258 | } | 
|---|
| 259 | } | 
|---|
| 260 | else | 
|---|
| 261 | { | 
|---|
| 262 | /* | 
|---|
| 263 | * Arguments: extract.exe | 
|---|
| 264 | * | 
|---|
| 265 | * Action:    Scan current directory for *.sym files. | 
|---|
| 266 | * | 
|---|
| 267 | */ | 
|---|
| 268 | USHORT      usSearch = 1; | 
|---|
| 269 | HDIR        hDir = HDIR_CREATE; | 
|---|
| 270 | FILEFINDBUF ffb; | 
|---|
| 271 | int         i; | 
|---|
| 272 |  | 
|---|
| 273 | printf16("/* $Id: extract.c,v 1.7 2002-12-06 02:55:58 bird Exp $\n" | 
|---|
| 274 | "*\n" | 
|---|
| 275 | "* Autogenerated kernel symbol database.\n" | 
|---|
| 276 | "*\n" | 
|---|
| 277 | "* Copyright (c) 2000 knut st. osmundsen (knut.stange.osmundsen@mynd.no)\n" | 
|---|
| 278 | "*\n" | 
|---|
| 279 | "* Project Odin Software License can be found in LICENSE.TXT\n" | 
|---|
| 280 | "*\n" | 
|---|
| 281 | "*/\n"); | 
|---|
| 282 |  | 
|---|
| 283 | printf16("\n" | 
|---|
| 284 | "#define INCL_NOPMAPI\n" | 
|---|
| 285 | "#define INCL_NOBASEAPI\n" | 
|---|
| 286 | "#include <os2.h>\n" | 
|---|
| 287 | "#include \"DevSegDf.h\"\n" | 
|---|
| 288 | "#include \"probkrnl.h\"\n" | 
|---|
| 289 | "#include \"options.h\"\n" | 
|---|
| 290 | "\n"); | 
|---|
| 291 |  | 
|---|
| 292 | printf16("KRNLDBENTRY DATA16_INIT aKrnlSymDB[] = \n" | 
|---|
| 293 | "{\n"); | 
|---|
| 294 |  | 
|---|
| 295 | rc = DosFindFirst("*.sym", &hDir, FILE_NORMAL, | 
|---|
| 296 | &ffb, sizeof(ffb), | 
|---|
| 297 | &usSearch, 0UL); | 
|---|
| 298 | while (rc == NO_ERROR & usSearch > 0) | 
|---|
| 299 | { | 
|---|
| 300 | rc = processFile(&ffb.achName[0]); | 
|---|
| 301 | if (rc != NO_ERROR) | 
|---|
| 302 | { | 
|---|
| 303 | printf16("processFile failed with rc=%d for file %s\n", | 
|---|
| 304 | rc, &ffb.achName[0]); | 
|---|
| 305 | if (psz = GetErrorMsg(rc)) | 
|---|
| 306 | printf16("%s\n", psz); | 
|---|
| 307 | return rc; | 
|---|
| 308 | } | 
|---|
| 309 |  | 
|---|
| 310 | /* next file */ | 
|---|
| 311 | rc = DosFindNext(hDir, &ffb, sizeof(ffb), &usSearch); | 
|---|
| 312 | } | 
|---|
| 313 | DosFindClose(hDir); | 
|---|
| 314 |  | 
|---|
| 315 | printf16("    { /* Terminating entry */\n" | 
|---|
| 316 | "        0,0,0,\n" | 
|---|
| 317 | "        {\n"); | 
|---|
| 318 | for (i = 0; i < NBR_OF_KRNLIMPORTS; i++) | 
|---|
| 319 | printf16("            {0,0},\n"); | 
|---|
| 320 | printf16("        }\n" | 
|---|
| 321 | "    }\n" | 
|---|
| 322 | "}; /* end of aKrnlSymDB[] */\n" | 
|---|
| 323 | ); | 
|---|
| 324 | } | 
|---|
| 325 |  | 
|---|
| 326 |  | 
|---|
| 327 | return rc; | 
|---|
| 328 | } | 
|---|
| 329 |  | 
|---|