00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include <stdio.h>
00034 #include <stdlib.h>
00035 #include <string.h>
00036 #include <unistd.h>
00037
00038 #include "pcihdr.h"
00039
00040 #include "toke.h"
00041 #include "vocabfuncts.h"
00042 #include "stack.h"
00043 #include "scanner.h"
00044 #include "emit.h"
00045 #include "clflags.h"
00046 #include "errhandler.h"
00047 #include "stream.h"
00048 #include "nextfcode.h"
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066 #define EMIT_STRUCT(x) {int j; for (j=0; j < sizeof(x) ; j++ ) emit_byte(0); }
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082 static int fcode_start_ob_off = -1;
00083 static int fcode_hdr_ob_off = -1;
00084 static int fcode_body_ob_off = -1;
00085 static int pci_hdr_ob_off = -1;
00086 static int pci_data_blk_ob_off = -1;
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096 static bool fcode_written = FALSE;
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108 extern u8 *ostart;
00109 extern int olen;
00110 extern void increase_output_buffer( void);
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121 void init_emit( void);
00122 void init_emit( void)
00123 {
00124 fcode_start_ob_off = -1;
00125 fcode_hdr_ob_off = -1;
00126 fcode_body_ob_off = -1;
00127 pci_hdr_ob_off = -1;
00128 pci_data_blk_ob_off = -1;
00129 fcode_written = FALSE;
00130 haveend = FALSE;
00131 }
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144 static void emit_byte(u8 data)
00145 {
00146 if ( opc == olen)
00147 {
00148 increase_output_buffer();
00149 }
00150
00151
00152 *(ostart+opc) = data ;
00153 opc++;
00154 }
00155
00156 void emit_fcode(u16 tok)
00157 {
00158 if ((tok>>8))
00159 emit_byte(tok>>8);
00160
00161 emit_byte(tok&0xff);
00162 fcode_written = TRUE;
00163 }
00164
00165 static void emit_num32(u32 num)
00166 {
00167 emit_byte(num>>24);
00168 emit_byte((num>>16)&0xff);
00169 emit_byte((num>>8)&0xff);
00170 emit_byte(num&0xff);
00171
00172 }
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183 void user_emit_byte(u8 data)
00184 {
00185 emit_byte( data);
00186 fcode_written = TRUE;
00187 }
00188
00189 void emit_offset(s16 offs)
00190 {
00191
00192 if (offs16)
00193 emit_byte(offs>>8);
00194 emit_byte(offs&0xff);
00195 }
00196
00197 void emit_literal(u32 num)
00198 {
00199 emit_token("b(lit)");
00200 emit_num32(num);
00201 }
00202
00203 void emit_string(u8 *string, signed int cnt)
00204 {
00205 signed int i=0;
00206 signed int cnt_cpy = cnt;
00207
00208 if ( cnt_cpy > STRING_LEN_MAX )
00209 {
00210 tokenization_error( TKERROR,
00211 "String too long: %d characters. Truncating.\n",cnt);
00212 cnt_cpy = STRING_LEN_MAX ;
00213 }
00214 emit_byte(cnt_cpy);
00215 for (i=0; i<cnt_cpy; i++)
00216 emit_byte(string[i]);
00217 }
00218
00219 void emit_fcodehdr(const char *starter_name)
00220 {
00221
00222
00223 if ( fcode_written )
00224 {
00225 tokenization_error( TKERROR ,
00226 "Cannot create FCode header after FCode output has begun.\n");
00227 if ( ! noerrors ) return ;
00228 }
00229
00230 fcode_header_t *fcode_hdr;
00231
00232 fcode_start_ob_off = opc;
00233 emit_token( starter_name );
00234
00235 fcode_hdr_ob_off = opc;
00236 fcode_hdr = (fcode_header_t *)(ostart+fcode_hdr_ob_off);
00237
00238 EMIT_STRUCT(fcode_header_t);
00239
00240 fcode_body_ob_off = opc;
00241
00242
00243 fcode_hdr->format = 0x08;
00244
00245 }
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268 void finish_fcodehdr(void)
00269 {
00270
00271 if( fcode_hdr_ob_off == -1 )
00272 {
00273 tokenization_error( TKERROR,
00274 "Missing FCode header.\n");
00275 return ;
00276 }
00277
00278
00279 if (!haveend)
00280 {
00281 tokenization_error ( WARNING,
00282 "End-of-file encountered without END0 or FCODE-END. "
00283 "Supplying END0\n");
00284 emit_token("end0");
00285 fcode_ender();
00286 }
00287
00288
00289 if ( fcode_start_ob_off != -1 )
00290 {
00291 u16 checksum;
00292 int length;
00293
00294 u8 *fcode_body = ostart+fcode_body_ob_off;
00295 u8 *ob_end = ostart+opc;
00296 fcode_header_t *fcode_hdr =
00297 (fcode_header_t *)(ostart+fcode_hdr_ob_off);
00298
00299 length = opc - fcode_start_ob_off;
00300
00301 for ( checksum = 0;
00302 fcode_body < ob_end ;
00303 checksum += *(fcode_body++) ) ;
00304
00305 BIG_ENDIAN_WORD_STORE(fcode_hdr->checksum , checksum);
00306 BIG_ENDIAN_LONG_STORE(fcode_hdr->length , length);
00307
00308 if (verbose)
00309 {
00310 printf( "toke: checksum is 0x%04x (%d bytes). ",
00311 checksum, length);
00312 list_fcode_ranges( TRUE);
00313 }
00314 }
00315
00316
00317 fcode_start_ob_off = -1;
00318 fcode_hdr_ob_off = -1;
00319 fcode_body_ob_off = -1;
00320 fcode_written = FALSE;
00321 haveend=FALSE;
00322 }
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352 static void emit_pci_rom_hdr(void)
00353 {
00354 rom_header_t *pci_hdr;
00355 pci_hdr_ob_off = opc;
00356 pci_hdr = (rom_header_t *)(ostart + pci_hdr_ob_off);
00357
00358 EMIT_STRUCT(rom_header_t);
00359
00360
00361 LITTLE_ENDIAN_WORD_STORE(pci_hdr->signature,0xaa55);
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378 LITTLE_ENDIAN_WORD_STORE( pci_hdr->reserved ,
00379 (sizeof(rom_header_t) + sizeof(pci_data_t)) ) ;
00380
00381
00382
00383
00384 LITTLE_ENDIAN_WORD_STORE(pci_hdr->data_ptr, sizeof(rom_header_t) );
00385
00386 }
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420 static void emit_pci_data_block(void)
00421 {
00422 pci_data_t *pci_data_blk;
00423 u32 class_id = dpop();
00424 u16 dev_id = dpop();
00425 u16 vend_id = dpop();
00426
00427 pci_data_blk_ob_off = opc;
00428 pci_data_blk = (pci_data_t *)(ostart + pci_data_blk_ob_off);
00429
00430 EMIT_STRUCT(pci_data_t);
00431
00432 BIG_ENDIAN_LONG_STORE(pci_data_blk->signature , PCI_DATA_HDR );
00433
00434 LITTLE_ENDIAN_WORD_STORE(pci_data_blk->vendor , vend_id );
00435 LITTLE_ENDIAN_WORD_STORE(pci_data_blk->device , dev_id );
00436 LITTLE_ENDIAN_TRIPLET_STORE(pci_data_blk->class_code , class_id );
00437
00438 LITTLE_ENDIAN_WORD_STORE(pci_data_blk->dlen , sizeof(pci_data_t) );
00439
00440 pci_data_blk->drevision = PCI_DATA_STRUCT_REV ;
00441
00442
00443 pci_data_blk->code_type = 1;
00444
00445
00446 pci_data_blk->last_image_flag = pci_is_last_image ? 0x80 : 0 ;
00447
00448 tokenization_error(INFO ,
00449 "PCI header vendor id=0x%04x, "
00450 "device id=0x%04x, class=%06x\n",
00451 vend_id, dev_id, class_id );
00452
00453 }
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480 void emit_pcihdr(void)
00481 {
00482
00483
00484 if (
00485
00486 ( fcode_start_ob_off != -1 )
00487
00488 || fcode_written
00489 )
00490 {
00491 tokenization_error( TKERROR ,
00492 "Cannot create PCI header after FCode output has begun.\n");
00493 if ( ! noerrors ) return ;
00494 }
00495
00496 emit_pci_rom_hdr();
00497
00498 emit_pci_data_block();
00499 }
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509 void finish_pcihdr(void)
00510 {
00511
00512 u32 imagesize ;
00513 u32 imageblocks;
00514 int padding;
00515
00516 rom_header_t *pci_hdr;
00517 pci_data_t *pci_data_blk;
00518
00519 if( pci_data_blk_ob_off == -1 )
00520 {
00521 tokenization_error( TKERROR,
00522 "%s without PCI-HEADER\n", strupr(statbuf) );
00523 return ;
00524 }
00525
00526 pci_hdr = (rom_header_t *)(ostart + pci_hdr_ob_off);
00527 pci_data_blk = (pci_data_t *)(ostart + pci_data_blk_ob_off);
00528
00529
00530 LITTLE_ENDIAN_WORD_STORE(pci_data_blk->vpd, pci_vpd);
00531
00532
00533 imagesize = opc - pci_hdr_ob_off;
00534 imageblocks = (imagesize + 511) >> 9;
00535 padding = (imageblocks << 9) - imagesize;
00536
00537
00538 LITTLE_ENDIAN_WORD_STORE(pci_data_blk->ilen, imageblocks);
00539
00540
00541 if ( big_end_pci_image_rev )
00542 {
00543 BIG_ENDIAN_WORD_STORE(pci_data_blk->irevision, pci_image_rev);
00544 }else{
00545 LITTLE_ENDIAN_WORD_STORE(pci_data_blk->irevision, pci_image_rev);
00546 }
00547
00548
00549 pci_data_blk->last_image_flag = pci_is_last_image ? 0x80 : 0 ;
00550
00551
00552
00553 printf("Adding %d bytes of zero padding to PCI image.\n",padding);
00554 while (padding--)
00555 emit_byte(0);
00556 if ( ! pci_is_last_image )
00557 {
00558 printf("Note: PCI header is not last image.\n");
00559 }
00560 printf("\n");
00561
00562 pci_hdr_ob_off = -1;
00563 pci_data_blk_ob_off = -1;
00564 }
00565
00566 void finish_headers(void)
00567 {
00568 if (fcode_hdr_ob_off != -1) finish_fcodehdr();
00569 if (pci_hdr_ob_off != -1) finish_pcihdr();
00570 }
00571