2N/A/* kern/cmos_datetime.c - CMOS datetime function.
2N/A *
2N/A * GRUB -- GRand Unified Bootloader
2N/A * Copyright (C) 2008,2009 Free Software Foundation, Inc.
2N/A *
2N/A * GRUB is free software: you can redistribute it and/or modify
2N/A * it under the terms of the GNU General Public License as published by
2N/A * the Free Software Foundation, either version 3 of the License, or
2N/A * (at your option) any later version.
2N/A *
2N/A * GRUB is distributed in the hope that it will be useful,
2N/A * but WITHOUT ANY WARRANTY; without even the implied warranty of
2N/A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2N/A * GNU General Public License for more details.
2N/A *
2N/A * You should have received a copy of the GNU General Public License
2N/A * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
2N/A */
2N/A
2N/A#include <grub/datetime.h>
2N/A#include <grub/ieee1275/ieee1275.h>
2N/A#include <grub/misc.h>
2N/A#include <grub/dl.h>
2N/A#if defined (__powerpc__) || defined (__sparc__)
2N/A#include <grub/cmos.h>
2N/A#endif
2N/A
2N/AGRUB_MOD_LICENSE ("GPLv3+");
2N/A
2N/Astatic char *rtc = 0;
2N/Astatic int no_ieee1275_rtc = 0;
2N/A
2N/Astatic void
2N/Afind_rtc (void)
2N/A{
2N/A auto int hook (struct grub_ieee1275_devalias *alias);
2N/A int hook (struct grub_ieee1275_devalias *alias)
2N/A {
2N/A if (grub_strcmp (alias->type, "rtc") == 0)
2N/A {
2N/A grub_dprintf ("datetime", "Found RTC %s\n", alias->path);
2N/A rtc = grub_strdup (alias->path);
2N/A return 1;
2N/A }
2N/A return 0;
2N/A }
2N/A
2N/A grub_ieee1275_devices_iterate (hook);
2N/A if (!rtc)
2N/A no_ieee1275_rtc = 1;
2N/A}
2N/A
2N/Agrub_err_t
2N/Agrub_get_datetime (struct grub_datetime *datetime)
2N/A{
2N/A struct get_time_args
2N/A {
2N/A struct grub_ieee1275_common_hdr common;
2N/A grub_ieee1275_cell_t method;
2N/A grub_ieee1275_cell_t device;
2N/A grub_ieee1275_cell_t catch_result;
2N/A grub_ieee1275_cell_t year;
2N/A grub_ieee1275_cell_t month;
2N/A grub_ieee1275_cell_t day;
2N/A grub_ieee1275_cell_t hour;
2N/A grub_ieee1275_cell_t minute;
2N/A grub_ieee1275_cell_t second;
2N/A }
2N/A args;
2N/A int status;
2N/A grub_ieee1275_ihandle_t ihandle;
2N/A
2N/A if (no_ieee1275_rtc)
2N/A return grub_get_datetime_cmos (datetime);
2N/A if (!rtc)
2N/A find_rtc ();
2N/A if (!rtc)
2N/A return grub_get_datetime_cmos (datetime);
2N/A
2N/A status = grub_ieee1275_open (rtc, &ihandle);
2N/A if (status == -1)
2N/A return grub_error (GRUB_ERR_IO, "couldn't open RTC");
2N/A
2N/A INIT_IEEE1275_COMMON (&args.common, "call-method", 2, 7);
2N/A args.device = (grub_ieee1275_cell_t) ihandle;
2N/A args.method = (grub_ieee1275_cell_t) "get-time";
2N/A
2N/A status = IEEE1275_CALL_ENTRY_FN (&args);
2N/A
2N/A grub_ieee1275_close (ihandle);
2N/A
2N/A if (status == -1)
2N/A return grub_error (GRUB_ERR_IO, "get-time failed");
2N/A
2N/A datetime->year = args.year;
2N/A datetime->month = args.month;
2N/A datetime->day = args.day;
2N/A datetime->hour = args.hour;
2N/A datetime->minute = args.minute;
2N/A datetime->second = args.second;
2N/A
2N/A return GRUB_ERR_NONE;
2N/A}
2N/A
2N/Agrub_err_t
2N/Agrub_set_datetime (struct grub_datetime *datetime)
2N/A{
2N/A struct set_time_args
2N/A {
2N/A struct grub_ieee1275_common_hdr common;
2N/A grub_ieee1275_cell_t method;
2N/A grub_ieee1275_cell_t device;
2N/A grub_ieee1275_cell_t year;
2N/A grub_ieee1275_cell_t month;
2N/A grub_ieee1275_cell_t day;
2N/A grub_ieee1275_cell_t hour;
2N/A grub_ieee1275_cell_t minute;
2N/A grub_ieee1275_cell_t second;
2N/A grub_ieee1275_cell_t catch_result;
2N/A }
2N/A args;
2N/A int status;
2N/A grub_ieee1275_ihandle_t ihandle;
2N/A
2N/A if (no_ieee1275_rtc)
2N/A return grub_set_datetime_cmos (datetime);
2N/A if (!rtc)
2N/A find_rtc ();
2N/A if (!rtc)
2N/A return grub_set_datetime_cmos (datetime);
2N/A
2N/A status = grub_ieee1275_open (rtc, &ihandle);
2N/A if (status == -1)
2N/A return grub_error (GRUB_ERR_IO, "couldn't open RTC");
2N/A
2N/A INIT_IEEE1275_COMMON (&args.common, "call-method", 8, 1);
2N/A args.device = (grub_ieee1275_cell_t) ihandle;
2N/A args.method = (grub_ieee1275_cell_t) "set-time";
2N/A
2N/A args.year = datetime->year;
2N/A args.month = datetime->month;
2N/A args.day = datetime->day;
2N/A args.hour = datetime->hour;
2N/A args.minute = datetime->minute;
2N/A args.second = datetime->second;
2N/A
2N/A status = IEEE1275_CALL_ENTRY_FN (&args);
2N/A
2N/A grub_ieee1275_close (ihandle);
2N/A
2N/A if (status == -1)
2N/A return grub_error (GRUB_ERR_IO, "set-time failed");
2N/A
2N/A return GRUB_ERR_NONE;
2N/A}