Skip to contentSkip to conversation

fix GRUB_TICKS_PER_SECOND in include/grub/efi/time.h

PATCH In review Alexandre Boeglin 2 participants 10 messages Feb 17, 2008 – Mar 1, 2008

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fix GRUB_TICKS_PER_SECOND in include/grub/efi/time.h

Hi,

This small patch fixes grub_get_rtc on EFI architecture. If I understood correctly, one minute is 60000 milliseconds, not 3600.

Alex

Index: ChangeLog
===================================================================
RCS file: /sources/grub/grub2/ChangeLog,v
retrieving revision 1.591

ChangeLog +5 -0
diff -u -p -r1.591 ChangeLog
@@ -1,3 +1,8 @@
+2008-02-17  Alexandre Boeglin  <[email protected]>
+
+	* include/grub/efi/time.h (GRUB_TICKS_PER_SECOND): One minute is 60 * 1000
+	milliseconds.
+
 2008-02-17  Robert Millan  <[email protected]>
 
 	* util/i386/pc/grub-setup.c (setup): In find_first_partition_start(),

Index: include/grub/efi/time.h
===================================================================
RCS file: /sources/grub/grub2/include/grub/efi/time.h,v retrieving revision 1.2

grub/efi/time.h +2 -2
diff -u -p -r1.2 time.h
@@ -1,6 +1,6 @@
 /*
  *  GRUB  --  GRand Unified Bootloader
- *  Copyright (C) 2006,2007  Free Software Foundation, Inc.
+ *  Copyright (C) 2006,2007,2008  Free Software Foundation, Inc.
  *
  *  GRUB is free software: you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
@@ -22,7 +22,7 @@
 #include <grub/symbol.h>
 
 /* This is destined to overflow when one minute passes by.  */
-#define GRUB_TICKS_PER_SECOND	((1UL << 31) / 60 / 60 * 2)
+#define GRUB_TICKS_PER_SECOND	((1UL << 31) / 60 / 1000 * 2)
 
 /* Return the real time in ticks.  */
 grub_uint32_t EXPORT_FUNC (grub_get_rtc) (void);
#1 lore raw
Quoted 1 line · Alexandre Boeglin

This small patch fixes grub_get_rtc on EFI architecture.

Ah, actually, it seems it's not good enough, as it still hangs on minute leaps...

Alex

#2 lore raw
Quoted 2 lines · Alexandre Boeglin

Ah, actually, it seems it's not good enough, as it still hangs on minute leaps...

This one seems to work (returning unix time), but I don't know if it's acceptable.

Alex

Index: include/grub/efi/time.h
===================================================================
RCS file: /sources/grub/grub2/include/grub/efi/time.h,v retrieving revision 1.2

grub/efi/time.h +2 -3
diff -u -r1.2 time.h
@@ -1,6 +1,6 @@
 /*
  *  GRUB  --  GRand Unified Bootloader
- *  Copyright (C) 2006,2007  Free Software Foundation, Inc.
+ *  Copyright (C) 2006,2007,2008  Free Software Foundation, Inc.
  *
  *  GRUB is free software: you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
@@ -21,8 +21,7 @@
 
 #include <grub/symbol.h>
 
-/* This is destined to overflow when one minute passes by.  */
-#define GRUB_TICKS_PER_SECOND	((1UL << 31) / 60 / 60 * 2)
+#define GRUB_TICKS_PER_SECOND	1
 
 /* Return the real time in ticks.  */
 grub_uint32_t EXPORT_FUNC (grub_get_rtc) (void);

Index: kern/efi/efi.c
===================================================================
RCS file: /sources/grub/grub2/kern/efi/efi.c,v
retrieving revision 1.10

efi/efi.c +34 -3
diff -u -r1.10 efi.c
@@ -194,15 +194,46 @@
 {
   grub_efi_time_t time;
   grub_efi_runtime_services_t *r;
+  grub_uint32_t rtc_time;
+  grub_uint16_t cur_year;
 
   r = grub_efi_system_table->runtime_services;
   if (r->get_time (&time, 0) != GRUB_EFI_SUCCESS)
     /* What is possible in this case?  */
     return 0;
 
-  return (((time.minute * 60 + time.second) * 1000
-	   + time.nanosecond / 1000000)
-	  * GRUB_TICKS_PER_SECOND / 1000);
+  rtc_time = 0;
+  for (cur_year = time.year -1; cur_year >= 1970; cur_year--)
+    {
+      if (!(cur_year % 400) || ((cur_year % 100) && !(cur_year % 4)))
+	rtc_time += 366;
+      else
+	rtc_time += 365;
+    }
+  /* Months have 30 days by default.  */
+  rtc_time += (time.month - 1) * 30;
+  /* February has fewer days.  */
+  if (time.month > 2)
+    {
+      if (!(time.year % 400) || ((time.year % 100) && !(time.year % 4)))
+	rtc_time -= 1;
+      else
+	rtc_time -= 2;
+    }
+  /* Odd months have 31 days.  */
+  rtc_time += time.month / 2;
+  /* August also has 31 days.  */
+  if (time.month == 9 || time.month == 11)
+    rtc_time += 1;
+  rtc_time += (time.day - 1);
+  rtc_time *= 24;
+  rtc_time += time.hour;
+  rtc_time *= 60;
+  rtc_time += time.minute;
+  rtc_time *= 60;
+  rtc_time += time.second;
+
+  return rtc_time;
 }
 
 /* Search the mods section from the PE32/PE32+ image. This code uses
#3 lore raw
Quoted 2 lines · Alexandre Boeglin

This one seems to work (returning unix time), but I don't know if it's acceptable.

Hi, here is the candidate for inclusion, including Changelog.

It has a resolution of 1 second, which is OK I think, as afaik grub_get_rtc is only used to display timeout counters.

And my machine, for example, does not even report more precise information (the struct returned by the EFI call has a nanosecond member, but it's content is always 0) ...

And for more precise time related operations, EFI offers a stall() call, which takes a number of microseconds as a parameters, IIRC.

Any objection ?

Alex

Index: ChangeLog
===================================================================
RCS file: /sources/grub/grub2/ChangeLog,v
retrieving revision 1.596

ChangeLog +7 -0
diff -u -p -r1.596 ChangeLog
@@ -1,3 +1,10 @@
+2008-02-21  Alexandre Boeglin  <[email protected]>
+
+	* kern/efi/efi.c (grub_get_rtc): New implementation that returns POSIX time.
+
+	* include/grub/efi/time.h (GRUB_TICKS_PER_SECOND): POSIX time has a
+	resolution of 1 second.
+
 2008-02-19  Pavel Roskin  <[email protected]>
 
 	* kern/rescue.c (grub_enter_rescue_mode): Improve initial

Index: include/grub/efi/time.h
===================================================================
RCS file: /sources/grub/grub2/include/grub/efi/time.h,v retrieving revision 1.2

grub/efi/time.h +2 -3
diff -u -p -r1.2 time.h
@@ -1,6 +1,6 @@
 /*
  *  GRUB  --  GRand Unified Bootloader
- *  Copyright (C) 2006,2007  Free Software Foundation, Inc.
+ *  Copyright (C) 2006,2007,2008  Free Software Foundation, Inc.
  *
  *  GRUB is free software: you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
@@ -21,8 +21,7 @@
 
 #include <grub/symbol.h>
 
-/* This is destined to overflow when one minute passes by.  */
-#define GRUB_TICKS_PER_SECOND	((1UL << 31) / 60 / 60 * 2)
+#define GRUB_TICKS_PER_SECOND	1
 
 /* Return the real time in ticks.  */
 grub_uint32_t EXPORT_FUNC (grub_get_rtc) (void);

Index: kern/efi/efi.c
===================================================================
RCS file: /sources/grub/grub2/kern/efi/efi.c,v
retrieving revision 1.10

efi/efi.c +34 -3
diff -u -p -r1.10 efi.c
@@ -194,15 +194,46 @@ grub_get_rtc (void)
 {
   grub_efi_time_t time;
   grub_efi_runtime_services_t *r;
+  grub_uint32_t rtc_time;
+  grub_uint16_t cur_year;
 
   r = grub_efi_system_table->runtime_services;
   if (r->get_time (&time, 0) != GRUB_EFI_SUCCESS)
     /* What is possible in this case?  */
     return 0;
 
-  return (((time.minute * 60 + time.second) * 1000
-	   + time.nanosecond / 1000000)
-	  * GRUB_TICKS_PER_SECOND / 1000);
+  rtc_time = 0;
+  for (cur_year = time.year - 1; cur_year >= 1970; cur_year--)
+    {
+      if (!(cur_year % 400) || ((cur_year % 100) && !(cur_year % 4)))
+	rtc_time += 366;
+      else
+	rtc_time += 365;
+    }
+  /* Months have 30 days by default.  */
+  rtc_time += (time.month - 1) * 30;
+  /* February has fewer days.  */
+  if (time.month > 2)
+    {
+      if (!(time.year % 400) || ((time.year % 100) && !(time.year % 4)))
+	rtc_time -= 1;
+      else
+	rtc_time -= 2;
+    }
+  /* Odd months have 31 days.  */
+  rtc_time += time.month / 2;
+  /* August also has 31 days.  */
+  if (time.month == 9 || time.month == 11)
+    rtc_time += 1;
+  rtc_time += (time.day - 1);
+  rtc_time *= 24;
+  rtc_time += time.hour;
+  rtc_time *= 60;
+  rtc_time += time.minute;
+  rtc_time *= 60;
+  rtc_time += time.second;
+
+  return rtc_time;
 }
 
 /* Search the mods section from the PE32/PE32+ image. This code uses
#4 lore raw
Quoted 2 lines · Alexandre Boeglin

It has a resolution of 1 second, which is OK I think, as afaik grub_get_rtc is only used to display timeout counters.

I don't think it is so good. In the future, we will have more active use of the timer (e.g. animation).

Quoted 3 lines

And my machine, for example, does not even report more precise information (the struct returned by the EFI call has a nanosecond member, but it's content is always 0) ...

I suggest that you should look at SetTimer. According to the spec, you can trigger an event periodically in the units of 10 nanoseconds. So this might be used to count a timer, if the precision is not too bad.

Otherwise, although this is very much hardware-dependent, you could use RDTSC. With the Stall call, you should be able to measure the clocks per second (or millisecond or whatever). Once you find the number of clocks per unit time, RDTSC gives you a very precise timer.

Okuji

#5 lore raw
In reply to Alexandre Boeglin
Quoted 2 lines · Alexandre Boeglin

This small patch fixes grub_get_rtc on EFI architecture. If I understood correctly, one minute is 60000 milliseconds, not 3600.

Nope. What's wrong here is the comment but not the code. It should read "This is destined to overflow when one hour passes by."

Okuji

#6 lore raw
Quoted 6 lines · Yoshinori K. Okuji
Quoted 2 lines · Alexandre Boeglin

This small patch fixes grub_get_rtc on EFI architecture. If I understood correctly, one minute is 60000 milliseconds, not 3600.

Nope. What's wrong here is the comment but not the code. It should read "This is destined to overflow when one hour passes by."

Then, I think the problem is the same, as the current grub_get_rtc returns a number of milliseconds. So, shouldn'it be 60 * 60 * 1000 ?

Alex

#7 lore raw
Quoted 10 lines · Alexandre Boeglin
Quoted 6 lines · Yoshinori K. Okuji
Quoted 2 lines · Alexandre Boeglin

This small patch fixes grub_get_rtc on EFI architecture. If I understood correctly, one minute is 60000 milliseconds, not 3600.

Nope. What's wrong here is the comment but not the code. It should read "This is destined to overflow when one hour passes by."

Then, I think the problem is the same, as the current grub_get_rtc returns a number of milliseconds. So, shouldn'it be 60 * 60 * 1000 ?

No. Why do you think it returns milliseconds?

Okuji

#8 lore raw
Quoted 4 lines · Yoshinori K. Okuji
Quoted 2 lines

Then, I think the problem is the same, as the current grub_get_rtc returns a number of milliseconds. So, shouldn'it be 60 * 60 * 1000 ?

No. Why do you think it returns milliseconds?

Ah, it does not exactly "return" milliseconds, sorry. But currently it seems GRUB_TICKS_PER_SECOND "overflows" a value in milliseconds, so I think it should use milliseconds and not seconds as its "base".

Alex

#9 lore raw
Quoted 9 lines · Alexandre Boeglin
Quoted 4 lines · Yoshinori K. Okuji
Quoted 2 lines

Then, I think the problem is the same, as the current grub_get_rtc returns a number of milliseconds. So, shouldn'it be 60 * 60 * 1000 ?

No. Why do you think it returns milliseconds?

Ah, it does not exactly "return" milliseconds, sorry. But currently it seems GRUB_TICKS_PER_SECOND "overflows" a value in milliseconds, so I think it should use milliseconds and not seconds as its "base".

I am sorry, but I don't understand. What problem do you really see?

Okuji