File: get-edid.c

package info (click to toggle)
read-edid 1.4.1-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 212 kB
  • ctags: 197
  • sloc: ansic: 1,334; sh: 335; makefile: 62
file content (401 lines) | stat: -rw-r--r-- 9,089 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/* (c) 2000,2001 John Fremlin */

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <sys/types.h>

#include "lrmi.h"

typedef u_int8_t byte;
typedef u_int32_t uint32;

typedef struct LRMI_regs reg_frame;
typedef byte* real_ptr;

#define access_register(reg_frame,reg) (reg_frame . reg)
#define access_ptr_register(reg_frame,reg) (reg_frame -> reg)
#define access_seg_register(reg_frame,es) reg_frame.es
#define real_mode_int(interrupt,reg_frame_ptr) !LRMI_int(interrupt,reg_frame_ptr)
#define alloc_real(size) LRMI_alloc_real(size)
#define free_real(block) LRMI_free_real(block)
#define dosmemput(buffer,length,offset) memcpy(offset,buffer,length)


real_ptr far_ptr_to_real_ptr( uint32 farptr )
{
  unsigned segment = (farptr & 0xffff0000)>>16;
  unsigned offset = (farptr & 0xffff);
  return (real_ptr)(segment*16+offset);
}




#define MAGIC   0x13

#define EDID_BLOCK_SIZE	128
#define EDID_V1_BLOCKS_TO_GO_OFFSET 126

#define SERVICE_REPORT_DDC	0
#define SERVICE_READ_EDID	1
#define SERVICE_LAST		1  // Read VDIF has been removed from the spec.



const char* ddc1_support_message[] =
  {
    "Monitor and video card combination does not support DDC1 transfers",
    "Monitor and video card combination supports DDC1 transfers"
  }
;
const char* ddc2_support_message[] =
  {
    "Monitor and video card combination does not support DDC2 transfers",
    "Monitor and video card combination supports DDC2 transfers"
  }
;
const char* screen_blanked_message[] =
  {
    "Screen is not blanked during DDC transfer",
    "Screen is blanked during DDC transfer"
  }
;


const char* supported_message[] =
  {
    "Function unsupported",
    "Function supported"
  }
;
const char* call_successful_message[] =
  {
    "Call failed",
    "Call successful"
  }
;

const char* vbe_service_message[] =
  {
    "Report DDC capabilities",
    "Read EDID"
  }
;

void
contact_author()
{
  fprintf( stderr,"\n\n*********** Something special has happened!\n" );
  fprintf( stderr,"Please contact the author, John Fremlin\n" );
  fprintf( stderr,"E-mail: one of vii@altern.org,vii@mailcc.com,vii@mailandnews.com\n" );
  fprintf( stderr,
	   "Please include full output from this program (especially that to stderr)\n\n\n\n" );
}


int
read_edid(  unsigned controller, FILE* output );

int do_vbe_ddc_service(unsigned BX,reg_frame* regs);

int
report_ddc_capabilities( unsigned controller );

int
report_vcontroller();
     

int
main( int argc, char** argv )
{
  unsigned controller = 0;
  int error;
  FILE* output;

  fprintf(stderr, "%s: get-edid version %s\n", argv[0], VERSION);
  
  if( !LRMI_init() )
    {
      fprintf( stderr, "%s: error initialising realmode interface\n", argv[0] );
      fprintf( stderr, "%s: do you have full superuser (root) permissions?\n", argv[0] );
      return 10;
    }
  
  ioperm(0, 0x400 , 1);
  iopl(3);
  

  if ( argc == 1 )
    output = stdout;
  else
    {
      if ( argc > 3 )
	{
	  fprintf( stderr, "%s: syntax %s [output-filename] [controller]\n",
		   argv[0],argv[0] );
	  return 3;
	}
      output = fopen( argv[1], "wb" );
      if ( !output )
	{
	  fprintf( stderr, "%s: error opening file \"%s\" for binary output\n",
		   argv[0],argv[1] );
	  return 4;
	}
      if ( argc == 3 )
	controller = strtol( argv[2],0,0 );
    }
  
  report_vcontroller();
  
  error = report_ddc_capabilities(controller);
  
  if (read_edid(controller,output))
    error = 1;

  fclose(output);
  
  return error;
}

int do_vbe_service(unsigned AX,unsigned BX,reg_frame* regs)
{
  const unsigned interrupt = 0x10;
  unsigned function_sup;
  unsigned success;
  int error = 0;

  access_ptr_register(regs,eax) = AX;
  access_ptr_register(regs,ebx) = BX;

  fprintf( stderr, "\n\tPerforming real mode VBE call\n" );

  fprintf( stderr, "\tInterrupt 0x%x ax=0x%x bx=0x%x cx=0x%x\n",
	   interrupt, AX, BX, (unsigned)access_ptr_register(regs,ecx) );
  
  if( real_mode_int(interrupt, regs) )
    {
      fprintf( stderr, "Error: something went wrong performing real mode interrupt\n" );
      error = 1;
    }
  
  AX = access_ptr_register(regs,eax);

  function_sup = ((AX & 0xff) == 0x4f);
  success = ((AX & 0xff00) == 0);
  
  fprintf( stderr, "\t%s\n", supported_message[ function_sup ] );
  fprintf( stderr, "\t%s\n\n", call_successful_message[ success ] );

  if (!success)
    error=1;
  if (!function_sup)
    error=2;
  
  return error;
}
  
int do_vbe_ddc_service(unsigned BX,reg_frame* regs)
{
  unsigned service = BX&0xff;
  unsigned AX = 0x4f15;


  fprintf( stderr, "\nVBE/DDC service about to be called\n" );
  if ( service > SERVICE_LAST )
    {
      fprintf( stderr, "\tUnknown VBE/DDC service\n" );
    }
  else
    {
      fprintf( stderr, "\t%s\n", vbe_service_message[ service ] );
    }  
  return do_vbe_service(AX,BX,regs);
}


int
report_ddc_capabilities( unsigned controller )
{
  reg_frame regs;
  int error;
  unsigned seconds_per_edid_block;
  unsigned ddc1_support;
  unsigned ddc2_support;
  unsigned screen_blanked_during_transfer;
  
  memset(&regs, 0, sizeof(regs));
  
  access_register(regs,ecx) = controller;
  
  error = do_vbe_ddc_service( SERVICE_REPORT_DDC,&regs );

  if ( !error )
    {
      seconds_per_edid_block = (access_register(regs,ebx) & 0xff00)>>16;
      ddc1_support = (access_register(regs,ebx) & 0x1)?1:0;
      ddc2_support = (access_register(regs,ebx) & 0x2)?1:0;
      screen_blanked_during_transfer = (access_register(regs,ebx) & 0x4)?1:0;

      fprintf( stderr, "\t%s\n", ddc1_support_message[ddc1_support] );
      fprintf( stderr, "\t%s\n", ddc2_support_message[ddc2_support] );
      fprintf( stderr, "\t%u seconds per 128 byte EDID block transfer\n",
	       seconds_per_edid_block );
      fprintf( stderr, "\t%s\n\n", screen_blanked_message[screen_blanked_during_transfer] );
    }
  
  return error;
}


int
read_edid(  unsigned controller, FILE* output )
{
  reg_frame regs;
 
  real_ptr block;
  byte* buffer;
  byte* pointer;
  unsigned blocks_left = 1;
  unsigned last_reported = 0x1000; /* just a large number */
  unsigned author_msg_shown = 0;
  int err = 0;

  block = alloc_real( EDID_BLOCK_SIZE );
  
  if ( !block )
    {
      fprintf( stderr, "Error: can't allocate 0x%x bytes of DOS memory for output block\n",
	       EDID_BLOCK_SIZE );
      return 2;
    }


  buffer = block;

  do
    {
      unsigned counter;

      fprintf( stderr, "Reading next EDID block\n" );
      memset( block, MAGIC, EDID_BLOCK_SIZE );
      memset(&regs, 0, sizeof(regs));

      access_seg_register(regs,es) = ((unsigned)block)/16;
      access_register(regs,edi) = 0;
      access_register(regs,ecx) = controller;
      access_register(regs,edx)= blocks_left;

      if ( do_vbe_ddc_service( SERVICE_READ_EDID, &regs ) )
	{
	  fprintf( stderr,
		   "The EDID data should not be trusted as the VBE call failed\n" );
	}
      
      for( pointer=block, counter=EDID_BLOCK_SIZE; counter; counter--,pointer++ )
	{
	  if ( *pointer != MAGIC )
	    goto block_ok;
	}
      fprintf( stderr, "Error: output block unchanged\n" );
      err = 1;
      break;

    block_ok:

      if ( EDID_BLOCK_SIZE != fwrite( buffer, sizeof( byte ), EDID_BLOCK_SIZE, output ) )
	{
	  fprintf( stderr, "\nError: problem writing output\n" );
	  err = 1;
	  break;
	}
      
      blocks_left--;
      if ( buffer[ EDID_V1_BLOCKS_TO_GO_OFFSET ] > blocks_left )
	{
	  int reported = buffer[ EDID_V1_BLOCKS_TO_GO_OFFSET ];
	  
	  fprintf( stderr, "EDID claims %u more blocks left\n", reported);
	  if ( last_reported <= reported
		    || reported == 0xff
		    /* 0xff frequently comes up in corrupt edids */
		    || reported == MAGIC
		    )
	    {
	      fprintf( stderr,
		       "EDID blocks left is wrong.\n"
		       "Your EDID is probably invalid.\n");
	    }
	  else {
	    if(!author_msg_shown) {
	      contact_author();
	      author_msg_shown = 1;
	    }

	    last_reported = reported;
	    blocks_left = reported;
	  }
	}
    } while( blocks_left );
  
  free_real( block );
  
  return err;
}

int
report_vcontroller()
{
  const unsigned info_block_length = 0x200;
  real_ptr block;
  real_ptr oem_string;
  reg_frame regs;
  byte* buffer;
  uint32 oem_string_ptr;
  byte next;
  unsigned vbe_version=0;

  memset(&regs, 0, sizeof(regs));

  block = alloc_real( info_block_length );
  if ( !block )
    {
      fprintf( stderr,
	       "Error: can't allocate %x bytes of DOS memory for videocard info block\n",
	       info_block_length );
      return 2;
    }
  
  buffer = block;

  dosmemput("VBE2",4,block);

  access_seg_register(regs,es) = ((unsigned)block)/16;
  access_register(regs,edi) = 0;
  
  do_vbe_service(0x4f00,0,&regs);

  memcpy( &vbe_version, buffer+4, 2 );
  memcpy( &oem_string_ptr, buffer+6, 4 );

  oem_string = far_ptr_to_real_ptr( oem_string_ptr );

  fprintf( stderr, "\tVBE version %x\n", vbe_version );
  fprintf( stderr, "\tVBE string at 0x%x \"", (unsigned)oem_string );
  
  for(;;)
    {
      next = *oem_string;
      if ( !next )
	break;
      fputc( next,stderr );

      oem_string++;
    }
  
  fprintf( stderr,"\"\n" );
  return 1;
}