Ejemplo n.º 1
0
void platform_main(int arg)
{
    rcc_clock_setup_hse(&clock_72mhz);

    rcc_periph_clock_enable(RCC_GPIOA);
    rcc_periph_clock_enable(RCC_GPIOB);

    // CAN pin
    gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIOA_CAN_RX | GPIOA_CAN_TX);
    gpio_set_output_options(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_100MHZ, GPIOA_CAN_RX | GPIOA_CAN_TX);
    gpio_set_af(GPIOA, GPIO_AF9, GPIOA_CAN_RX | GPIOA_CAN_TX);

    // enable CAN transceiver
    gpio_mode_setup(GPIOA, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIOA_CAN_DIS);
    gpio_set_output_options(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_100MHZ, GPIOA_CAN_DIS);
    gpio_clear(GPIOA, GPIOA_CAN_DIS);

    // LED on
    gpio_mode_setup(GPIOA, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIOA_LED);
    gpio_set_output_options(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_100MHZ, GPIOA_LED);
    gpio_set(GPIOA, GPIOA_LED);

    // configure timeout of 10000 milliseconds
    timeout_timer_init(72000000, 10000);

    can_interface_init();

    bootloader_main(arg);

    reboot_system(BOOT_ARG_START_BOOTLOADER);
}
Ejemplo n.º 2
0
void EXTI4_15_IRQHandler(void)
{
    if (EXTI_GetIntBitState(CHARGING_EXTILINE) != RESET) {
        if (check_charging() == CHARGING && get_charger_plug_flag() == CHARGER_UNPLUGGED) {
            set_charger_plug_flag();
            reboot_system(SUCCESS);
        }
        EXTI_ClearIntBitState(CHARGING_EXTILINE);
    }
}
Ejemplo n.º 3
0
void I2C_GetBitState_Timeout(I2C_TypeDef* I2Cx, uint32_t I2C_FLAG)
{
    reset_timout_tic();
    while(I2C_GetBitState(I2Cx, I2C_FLAG) && timeout_tic > 0)
    {
        timeout_tic --;
    }
    if (timeout_tic == 0) {
        reboot_system(SUCCESS);
    }
}
Ejemplo n.º 4
0
void I2C_StateDetect_Timeout(I2C_TypeDef* I2Cx, u32 state)
{
    reset_timout_tic();
    while(!I2C_StateDetect(I2Cx, state) && timeout_tic > 0)
    {
        timeout_tic --;
    }
    if (timeout_tic == 0) {
        reboot_system(SUCCESS);
    }
}
Ejemplo n.º 5
0
void IIC_Stop(void)
{
    /* Send a stop condition to I2C bus */
    I2C_StopOnBus_Enable(BOARD_I2C,ENABLE);
#ifdef DBG_OLED_BLOCK
    while(BOARD_I2C->CTLR1&0x0200);
#else
    reset_timout_tic();
    while(BOARD_I2C->CTLR1&0x0200 && timeout_tic > 0) {
        timeout_tic--;
    }
    if (timeout_tic == 0) {
        reboot_system(SUCCESS);
    }
#endif

}
Ejemplo n.º 6
0
u8 power_voltage_confirm_key_repeat_action(void* key_t)
{
    key_state * key = (key_state *) key_t;
    if (key->repeat_count == 3 && get_system_state() == STATE_NORMAL) {
        switch_to_ui_page(SMOKE_MODE_SELECT_ID);
    }

    if (key->repeat_count == 5) {
        if (get_system_state() == STATE_CHARGING) {
            reboot_system(SUCCESS);
        } else {
            set_system_state(STATE_SLEEP);
        }
    }

    // We need to clear the repeat count after we finish the action.
    key->repeat_count = 0;
    return SUCCESS;
}
Ejemplo n.º 7
0
void smoke_mode_select_confirm_key_callback(u8 long_press)
{
    switch(current_index_value) {
    case MENU_SMOKE_POWER_MODE:
        switch_to_ui_page(MAIN_MENU_POWER_ID);
        break;
    /*
    case MENU_SMOKE_VOLTAGE_MODE:
        switch_to_ui_page(MAIN_MENU_VOLTAGE_ID);
        break;
    case MENU_SMOKE_BYPASS_MODE:
        switch_to_ui_page(MAIN_MENU_BYPASS_ID);
        break;
    */
    case MENU_SMOKE_TEMPERATURE_MODE:
        if (long_press == ERROR)
            switch_to_ui_page(TEMERATURE_MODE_SELECT_ID);
        else if (long_press == SUCCESS)
            switch_to_ui_page(get_temperature_mode_ui_id());
        break;
    case MENU_SMOKE_KEY_LOCK:
        set_value_key_lock_reverse();
        system_init();
        break;
#ifdef SCREEN_069
    case MENU_SMOKE_SCREEN_FLIP:
        screen_flip_flag_revert();
        reboot_system(SUCCESS);
        break;
#endif
    case MENU_SMOKE_ABOUT:
        switch_to_ui_page(MENU_ABOUT_ID);
        break;
    }

}
Ejemplo n.º 8
0
Archivo: runit.c Proyecto: Gottox/runit
int main (int argc, const char * const *argv, char * const *envp) {
  const char * prog[2];
  int pid, pid2;
  int wstat;
  int st;
  iopause_fd x;
#ifndef IOPAUSE_POLL
  fd_set rfds;
  struct timeval t;
#endif
  char ch;
  int ttyfd;
  struct stat s;

  if (getpid() != 1) strerr_die2x(111, FATAL, "must be run as process no 1.");
  setsid();

  sig_block(sig_alarm);
  sig_block(sig_child);
  sig_catch(sig_child, sig_child_handler);
  sig_block(sig_cont);
  sig_catch(sig_cont, sig_cont_handler);
  sig_block(sig_hangup);
  sig_block(sig_int);
  sig_catch(sig_int, sig_int_handler);
  sig_block(sig_pipe);
  sig_block(sig_term);

  /* console */
  if ((ttyfd =open_write("/dev/console")) != -1) {
    dup2(ttyfd, 0); dup2(ttyfd, 1); dup2(ttyfd, 2);
    if (ttyfd > 2) close(ttyfd);
  }

  /* create selfpipe */
  while (pipe(selfpipe) == -1) {
    strerr_warn2(FATAL, "unable to create selfpipe, pausing: ", &strerr_sys);
    sleep(5);
  }
  coe(selfpipe[0]);
  coe(selfpipe[1]);
  ndelay_on(selfpipe[0]);
  ndelay_on(selfpipe[1]);

#ifdef RB_DISABLE_CAD
  /* activate ctrlaltdel handling, glibc, dietlibc */
  if (RB_DISABLE_CAD == 0) reboot_system(0);
#endif

  strerr_warn3(INFO, "$Id: 25da3b86f7bed4038b8a039d2f8e8c9bbcf0822b $",
               ": booting.", 0);

  /* runit */
  for (st =0; st < 3; st++) {
    /* if (st == 2) logwtmp("~", "reboot", ""); */
    while ((pid =fork()) == -1) {
      strerr_warn4(FATAL, "unable to fork for \"", stage[st], "\" pausing: ",
                   &strerr_sys);
      sleep(5);
    }
    if (!pid) {
      /* child */
      prog[0] =stage[st];
      prog[1] =0;

      /* stage 1 gets full control of console */
      if (st == 0) {
        if ((ttyfd =open("/dev/console", O_RDWR)) != -1) {
#ifdef TIOCSCTTY 
          ioctl(ttyfd, TIOCSCTTY, (char *)0);
#endif
          dup2(ttyfd, 0);
          if (ttyfd > 2) close(ttyfd);
        }
        else
          strerr_warn2(WARNING, "unable to open /dev/console: ", &strerr_sys);
      }
      else
        setsid();

      sig_unblock(sig_alarm);
      sig_unblock(sig_child);
      sig_uncatch(sig_child);
      sig_unblock(sig_cont);
      sig_ignore(sig_cont);
      sig_unblock(sig_hangup);
      sig_unblock(sig_int);
      sig_uncatch(sig_int);
      sig_unblock(sig_pipe);
      sig_unblock(sig_term);
            
      strerr_warn3(INFO, "enter stage: ", stage[st], 0);
      execve(*prog, (char *const *)prog, envp);
      strerr_die4sys(0, FATAL, "unable to start child: ", stage[st], ": ");
    }

    x.fd =selfpipe[0];
    x.events =IOPAUSE_READ;
    for (;;) {
      int child;

      sig_unblock(sig_child);
      sig_unblock(sig_cont);
      sig_unblock(sig_int);
#ifdef IOPAUSE_POLL
      poll(&x, 1, 14000);
#else
      t.tv_sec =14; t.tv_usec =0;
      FD_ZERO(&rfds);
      FD_SET(x.fd, &rfds);
      select(x.fd +1, &rfds, (fd_set*)0, (fd_set*)0, &t);
#endif
      sig_block(sig_cont);
      sig_block(sig_child);
      sig_block(sig_int);
      
      while (read(selfpipe[0], &ch, 1) == 1) {}
      while ((child =wait_nohang(&wstat)) > 0)
        if (child == pid) break;
      if (child == -1) {
        strerr_warn2(WARNING, "wait_nohang, pausing: ", &strerr_sys);
        sleep(5);
      }

      /* reget stderr */
      if ((ttyfd =open_write("/dev/console")) != -1) {
        dup2(ttyfd, 2);
        if (ttyfd > 2) close(ttyfd);
      }

      if (child == pid) {
        if (wait_exitcode(wstat) != 0) {
          if (wait_crashed(wstat))
            strerr_warn3(WARNING, "child crashed: ", stage[st], 0);
          else
            strerr_warn3(WARNING, "child failed: ", stage[st], 0);
          if (st == 0)
            /* this is stage 1 */
            if (wait_crashed(wstat) || (wait_exitcode(wstat) == 100)) {
              strerr_warn3(INFO, "leave stage: ", stage[st], 0);
              strerr_warn2(WARNING, "skipping stage 2...", 0);
              st++;
              break;
            }
          if (st == 1)
            /* this is stage 2 */
            if (wait_crashed(wstat) || (wait_exitcode(wstat) == 111)) {
              strerr_warn2(WARNING, "killing all processes in stage 2...", 0);
              kill(-pid, 9);
              sleep(5);
              strerr_warn2(WARNING, "restarting.", 0);
              st--;
              break;
            }
        }
        strerr_warn3(INFO, "leave stage: ", stage[st], 0);
        break;
      }
      if (child != 0) {
        /* collect terminated children */
        write(selfpipe[1], "", 1);
        continue;
      }

      /* sig? */
      if (!sigc  && !sigi) {
#ifdef DEBUG
        strerr_warn2(WARNING, "poll: ", &strerr_sys);
#endif
        continue;
      }
      if (st != 1) {
        strerr_warn2(WARNING, "signals only work in stage 2.", 0);
        sigc =sigi =0;
        continue;
      }
      if (sigi && (stat(CTRLALTDEL, &s) != -1) && (s.st_mode & S_IXUSR)) {
        strerr_warn2(INFO, "ctrl-alt-del request...", 0);
        prog[0] =CTRLALTDEL; prog[1] =0;
        while ((pid2 =fork()) == -1) {
          strerr_warn4(FATAL, "unable to fork for \"", CTRLALTDEL,
                       "\" pausing: ", &strerr_sys);
          sleep(5);
        }
        if (!pid2) {
          /* child */
          strerr_warn3(INFO, "enter stage: ", prog[0], 0);
          execve(*prog, (char *const *) prog, envp);
          strerr_die4sys(0, FATAL, "unable to start child: ", prog[0], ": ");
        }
        if (wait_pid(&wstat, pid2) == -1)
          strerr_warn2(FATAL, "wait_pid: ", &strerr_sys);
        if (wait_crashed(wstat))
          strerr_warn3(WARNING, "child crashed: ", CTRLALTDEL, 0);
        strerr_warn3(INFO, "leave stage: ", prog[0], 0);
        sigi =0;
        sigc++;
      }
      if (sigc && (stat(STOPIT, &s) != -1) && (s.st_mode & S_IXUSR)) {
        int i;
        /* unlink(STOPIT); */
        chmod(STOPIT, 0);

        /* kill stage 2 */
#ifdef DEBUG
        strerr_warn2(WARNING, "sending sigterm...", 0);
#endif
        kill(pid, sig_term);
        i =0;
        while (i < 5) {
          if ((child =wait_nohang(&wstat)) == pid) {
#ifdef DEBUG
            strerr_warn2(WARNING, "stage 2 terminated.", 0);
#endif
            pid =0;
            break;
          }
          if (child) continue;
          if (child == -1) 
            strerr_warn2(WARNING, "wait_nohang: ", &strerr_sys);
#ifdef DEBUG
          strerr_warn2(WARNING, "waiting...", 0);
#endif
          sleep(1);
          i++;
        }
        if (pid) {
          /* still there */
          strerr_warn2(WARNING,
                       "stage 2 not terminated, sending sigkill...", 0);
          kill(pid, 9);
          if (wait_pid(&wstat, pid) == -1)
            strerr_warn2(WARNING, "wait_pid: ", &strerr_sys);
        }
        sigc =0;
        strerr_warn3(INFO, "leave stage: ", stage[st], 0);

        /* enter stage 3 */
        break;
      }
      sigc =sigi =0;
#ifdef DEBUG
      strerr_warn2(WARNING, "no request.", 0);
#endif
    }
  }

  /* reget stderr */
  if ((ttyfd =open_write("/dev/console")) != -1) {
    dup2(ttyfd, 2);
    if (ttyfd > 2) close(ttyfd);
  }

#ifdef RB_AUTOBOOT
  /* fallthrough stage 3 */
  strerr_warn2(INFO, "sending KILL signal to all processes...", 0);
  kill(-1, SIGKILL);

  pid =fork();
  switch (pid) {
  case  0:
  case -1:
  if ((stat(REBOOT, &s) != -1) && (s.st_mode & S_IXUSR)) {
    strerr_warn2(INFO, "system reboot.", 0);
    sync();
    reboot_system(RB_AUTOBOOT);
  }
  else {
#ifdef RB_POWER_OFF
    strerr_warn2(INFO, "power off...", 0);
    sync();
    reboot_system(RB_POWER_OFF);
    sleep(2);
#endif
#ifdef RB_HALT_SYSTEM
    strerr_warn2(INFO, "system halt.", 0);
    sync();
    reboot_system(RB_HALT_SYSTEM);
#else
#ifdef RB_HALT
    strerr_warn2(INFO, "system halt.", 0);
    sync();
    reboot_system(RB_HALT);
#else
    strerr_warn2(INFO, "system reboot.", 0);
    sync();
    reboot_system(RB_AUTOBOOT);
#endif
#endif
  }
  if (pid == 0) _exit(0);
  break;
  default:
  sig_unblock(sig_child);
  while (wait_pid(0, pid) == -1);
  }
#endif

  for (;;) sig_pause();
  /* not reached */
  strerr_die2x(0, INFO, "exit.");
  return(0);
}
Ejemplo n.º 9
0
u8 power_voltage_confirm_key_long_event_action(void* key_t)
{
    key_state* key = (key_state*) key_t;
    if (key->type != KEY_CONFIRM) {
        return ERROR;
    }

    if (key->state == KEY_ON) {
        if (get_system_state() == STATE_CHARGING)
            return ERROR;

        // pre deal the register issue.
        if ((key->press_keep_tic >= CONFIRM_KEY_PREPARE_TIME) && get_pid_lock() == PID_UNLOCK) {
            set_pid_lock(PID_LOCK);
        }

        // deal with press / long press. We all need to report time.
        if (key->press_report_tic >= key->report_tic_interval || key->press_report_tic == 0) {
            key->press_report_tic = 0;
            if ((key->press_keep_tic >= CONFIRM_KEY_PREPARE_TIME) && ((key->press_keep_tic >= key->long_press_interval) || get_system_smoke() == SYS_SMOKE) && (get_system_smoke() != SYS_SMOKE_DONE)) {
                if (get_smoke_start_tic() == SMOKE_START_DUMMY_TIC) {
                    set_smoke_start_tic(key->press_keep_tic);
                    smoke_time_record = 0;
                }
                // HW check. We need to check all the HW before firing
                if (check_hw_fire() == ERROR) {
                    // clear flag
                    set_pid_lock(PID_UNLOCK);
                    reset_smoke_start_tic();
                    send_pid_command(CMD_STOP, PID_RESET);
                    set_system_smoke(SYS_SMOKE_DONE);
                    return ERROR;
                }
                if ((key->press_keep_tic - get_smoke_start_tic()) <= SMOKE_MAX_TIME_NUMBER) {
                    key->repeat_count = 0;
                    send_pid_command(CMD_SMOKE, PID_NOT_RESET);
                    draw_smoke_time((key->press_keep_tic - get_smoke_start_tic()) * TIMER_PERIOD / 100);
                    if ((key->press_keep_tic - get_smoke_start_tic()) >= (smoke_time_record)*SMOKE_RT_PARA_UPDATE_INTERVAL) {
                        draw_current_value(REGISTER_VALUE);
                        if (get_current_ui_id() == MAIN_MENU_BYPASS_ID)
                            draw_current_value(PID_POWER_RT_OUT_VALUE);
                        else
                            draw_current_value(VOLTAGE_VALUE);
                        smoke_time_record++;
                    }
                    // QIDONG
                    // Power ... Draw Voltage...
                } else { // exceed the max time.
                    // Redraw the MAX SMOKE TIME in case that the (key->press_report_tic - key->long_press_interval > MAX_TIME_NUMER) jump from < to > without ==
                    set_pid_lock(PID_UNLOCK);
                    draw_smoke_time(SMOKE_MAX_TIME_NUMBER * TIMER_PERIOD / 100);
                    send_pid_command(CMD_STOP, PID_RESET);
                    set_system_smoke(SYS_SMOKE_DONE);
                }
            }
        }
    } else if (key->state == KEY_OFF) {
        if (key->release_keep_tic > key->repeat_tic_interval) {
            power_voltage_confirm_key_repeat_action(key_t);
        } else if (key->repeat_count >= 5){
            if (get_system_state() == STATE_CHARGING) {
                reboot_system(SUCCESS);
            } else {
                set_system_state(STATE_SLEEP);
            }
        }
    }
    return SUCCESS;
}
Ejemplo n.º 10
0
static gint32
mca_exec_svr_command (guint32 cmd, void *data, guint32 datalen)
{
  gint     retval = 0;
  gint32   nr_data_h = 0;
  
  DEBUG_PRINT("mca_exec_command(): cmd=%08X [%d bytes]\n", cmd, datalen);

  if (datalen > 0)
    nr_data_h = ntohl(*(guint32 *)data);
  switch (cmd) {
    //server-to-client message/command
  case MKW_OWED_CASH:      //CS_SETOWED:
    DBG_PRINT("mca_exec_command(): cmd = MKW_OWED_CASH: [%d]\n", nr_data_h);
    set_owed_cash(nr_data_h);
    break;
  case MKW_PRODUCTS_CASH:   //CS_SETADDITIONAL:
    DBG_PRINT("mca_exec_command(): cmd = MKW_PRODUCTS_CASH: [%d]\n", datalen);
    set_products_cash(nr_data_h);
    break;
  case MKW_SESSION_TIME:    //CS_SETTIME:
    DBG_PRINT("mca_exec_command(): cmd = MKW_SESSION_TIME: [%d]\n", datalen);
    set_time_len (nr_data_h);
    break;
  case MKW_SERVER_MESSAGE: //CS_DISPLAYMESSAGE:
    DBG_PRINT("mca_exec_command(): cmd = MKW_SERVER_MESSAGE: [%d]\n", datalen);
    show_server_message((gchar *)data);
    break;
  case MKW_ACK_ASSIST:        //CS_CALLASSIST:
    DBG_PRINT("mca_exec_command(): cmd = MKW_ACK_ASSIST: [%d]\n", datalen);
    ack_assist_request((gchar *) data, datalen);
    break;
    //session control
  case MKW_STOP_SESSION: //CS_STOP:
    DBG_PRINT("mca_exec_command(): cmd = MKW_STOP_SESSION: [%d]\n", datalen);
    stop_session();
    break;
  case MKW_START_SESSION: //CS_START:
    DBG_PRINT("mca_exec_command(): cmd = MKW_START_SESSION: [%d]\n", datalen);
    start_session();
    break;
  case MKW_PAUSE_SESSION: //CS_PAUSE:
    DBG_PRINT("mca_exec_command(): cmd = MKW_PAUSE_SESSION: [%d]\n", datalen);
    pause_session();
    break;
  case MKW_RESUME_SESSION: //CS_RESUME:
    DBG_PRINT("mca_exec_command(): cmd = MKW_RESUME_SESSION: [%d]\n", datalen);
    resume_session();
    break;
  case MKW_TIMEOUT_SESSION:   //CS_SETTIMEOUT:
    DBG_PRINT("mca_exec_command(): cmd = MKW_TIMEOUT_SESSION: [%d]\n", datalen);
    start_session_timeout(nr_data_h);
    break;
    //system control
  case MKW_BLANK_MONITOR: //CS_MONITOROFF:
    DBG_PRINT("mca_exec_command(): cmd = MKW_BLANK_MONITOR: [%d]\n", datalen);
    blank_monitor();
    break;
  case MKW_SHUTDOWN_SYSTEM: // CS_SHUTDOWN:
    DBG_PRINT("mca_exec_command(): cmd = MKW_SHUTDOWN_SYSTEM: [%d]\n", datalen);
    shutdown_system();
    break;
  case MKW_REBOOT_SYSTEM:   //CS_REBOOT:
    DBG_PRINT("mca_exec_command(): cmd = MKW_REBOOT_SYSTEM: [%d]\n", datalen);
    reboot_system();
    break;
  case MKW_EXIT_MCA:     //CS_QUITCLIENT:
    DBG_PRINT("mca_exec_command(): cmd = MKW_EXIT_MCA: [%d]\n", datalen);
    exit_mca_program();
    break;
  case MKW_SET_ADMIN_PASSWD: //CS_SETADMINPASS:
    DBG_PRINT("mca_exec_command(): cmd = MKW_SET_ADMIN_PASSWD: [%d]\n", datalen);
    set_admin_passwd((gchar *)data, datalen);
    break;
  case MKW_SET_POLL_INTERVAL:   //  CS_SETPOLLINTERVAL:
    DBG_PRINT("mca_exec_command(): cmd = MKW_SET_POLL_INTERVAL: [%d]\n", datalen);
    set_poll_interval( nr_data_h  );
    break;
    //utils 
  case MKW_UNBLOCK_SCREEN : //CS_UNLOCKSCREEN:
    DBG_PRINT("mca_exec_command(): cmd = MKW_UNBLOCK_SCREEN: [%d]\n", datalen);
    unblock_screen();
    break;
  case MKW_BLOCK_SCREEN:    //CS_LOCKSCREEN:
    DBG_PRINT("mca_exec_command(): cmd = MKW_BLOCK_SCREEN: [%d]\n", datalen);
    block_screen();
    break;
    //UI and program state
  case MKW_ENABLE_PASSWD_BUTTON:    //CS_ENABLEPASSWORDEDIT:
    DBG_PRINT("mca_exec_command(): cmd = MKW_ENABLE_PASSWD_BUTTON: [%d]\n", datalen);
    set_passwd_edit_state(nr_data_h);
    break;
  case MKW_ALLOW_MEMBER_LOGIN:  // CS_ALLOWMEMBERLOGIN:
    DBG_PRINT("mca_exec_command(): cmd = MKW_ALLOW_MEMBER_LOGIN: [%d]\n", datalen);
    set_member_loginable(nr_data_h);
    break;
  case MKW_ALLOW_TICKET_LOGIN:  //CS_ALLOWTICKETLOGIN:
    DBG_PRINT("mca_exec_command(): cmd = MKW_ALLOW_TICKET_LOGIN: [%d]\n", datalen);
    set_ticket_loginable(nr_data_h);
    break;
  case MKW_ALLOW_USER_LOGIN: //CS_ALLOWUSERLOGIN:
    DBG_PRINT("mca_exec_command(): cmd = MKW_ALLOW_USER_LOGIN: [%d]\n", datalen);
    set_user_loginable(nr_data_h);
    break;
  case MKW_ENABLE_ASSIST_BUTTON: //CS_ENABLEASSIST:
    DBG_PRINT("mca_exec_command(): cmd = MKW_ENABLE_ASSIST_BUTTON: [%d]\n", datalen);
    set_assist_able(nr_data_h);
    break;
    //update
  case MKW_START_UPDATE:    //CS_UPDATE:
    DBG_PRINT("mca_exec_command(): cmd = MKW_START_UPDATE: [%d]\n", datalen);
    start_mkw_update ((gchar *) data, datalen);
    break;
  case MKW_UPDATE_DATA:  // CS_UPDATEDATA:
    DBG_PRINT("mca_exec_command(): cmd = MKW_UPDATE_DATA: [%d]\n", datalen);
    proc_mkw_update_data((gchar *)data, datalen);
    break;
  case MKW_END_UPDATE:  //CS_UPDATEEND:
    DBG_PRINT("mca_exec_command(): cmd = MKW_END_UPDATE: [%d]\n", datalen);
    end_mkw_update((gchar *)data, datalen);
    break;
  }

  return retval;
}
Ejemplo n.º 11
0
int main(int argc, char *argv[])
{
	struct sigaction act;
	int rc, rc1, rc2, readonly, clobbercfg;

	clobbercfg = readonly = 0;

	openlog("flatfsd", LOG_PERROR, LOG_DAEMON);

	while ((rc = getopt(argc, argv, "vcnribwH123hs?")) != EOF) {
		switch (rc) {
		case 'w':
			clobbercfg++;
			readonly++;
			break;

		case 'r':
			readonly++;
			break;
		case 'n':
			nowrite = 1;
			break;
		case 'c':
#if !defined(USING_FLASH_FILESYSTEM)
			rc = flat_check();
			if (rc < 0) {
#ifdef LOGGING
				char ecmd[64];
				sprintf(ecmd, "/bin/logd chksum-bad %d", -rc);
				system(ecmd);
#endif
				printf("Flash filesystem is invalid %d - check syslog\n", rc);
			} else {
				printf("Flash filesystem is valid\n");
#ifdef LOGGING
				system("/bin/logd chksum-good");
#endif
			}
			exit(rc);
#else
			exit(0);
#endif
			break;
		case 'v':
			version();
			exit(0);
			break;
		case 's':
			log_caller("/bin/logd flatfsd-s");
			exit(saveconfig());
			break;
		case 'b':
			log_caller("/bin/logd flatfsd-b");
			exit(reboot_system());
			break;
		case 'H':
			log_caller("/bin/logd flatfsd-h");
			exit(halt_system());
			break;
		case 'i':
			log_caller("/bin/logd flatfsd-i");
			exit(reset_config());
			break;
		case '1':
			fsver = 1;
			break;
		case '2':
			fsver = 2;
			break;
		case '3':
			fsver = 3;
			break;
		case 'h':
		case '?':
			usage(0);
			break;
		default:
			usage(1);
			break;
		}
	}

	if (readonly) {
		rc1 = rc2 = 0;

		if (clobbercfg ||
#if !defined(USING_FLASH_FILESYSTEM)
			((rc = flat_restorefs()) < 0) ||
#endif
			(rc1 = flat_filecount()) <= 0 ||
			(rc2 = flat_needinit())
		) {
#ifdef LOGGING
			char ecmd[64];

			/* log the reason we have for killing the flatfs */
			if (clobbercfg)
				sprintf(ecmd, "/bin/logd newflatfs clobbered");
			else if (rc < 0)
				sprintf(ecmd, "/bin/logd newflatfs recreate=%d", rc);
			else if (rc1 <= 0)
				sprintf(ecmd, "/bin/logd newflatfs filecount=%d", rc1);
			else if (rc2)
				sprintf(ecmd, "/bin/logd newflatfs needinit");
			else
				sprintf(ecmd, "/bin/logd newflatfs unknown");

			system(ecmd);
#endif
			syslog(LOG_ERR, "Nonexistent or bad flatfs (%d), creating new one...", rc);
			flat_clean(1);
			if ((rc = flat_new(DEFAULTDIR)) < 0) {
				syslog(LOG_ERR, "Failed to create new flatfs, err=%d errno=%d",
					rc, errno);
				exit(1);
			}
			save_config_to_flash();
		}
		syslog(LOG_INFO, "Created %d configuration files (%d bytes)",
			numfiles, numbytes);
		exit(0);
	}

	creatpidfile();

	act.sa_handler = sighup;
	memset(&act.sa_mask, 0, sizeof(act.sa_mask));
	act.sa_flags = SA_RESTART;
	act.sa_restorer = 0;
	sigaction(SIGHUP, &act, NULL);

	act.sa_handler = sigusr1;
	memset(&act.sa_mask, 0, sizeof(act.sa_mask));
	act.sa_flags = SA_RESTART;
	act.sa_restorer = 0;
	sigaction(SIGUSR1, &act, NULL);

	act.sa_handler = sigusr2;
	memset(&act.sa_mask, 0, sizeof(act.sa_mask));
	act.sa_flags = SA_RESTART;
	act.sa_restorer = 0;
	sigaction(SIGUSR2, &act, NULL);

	act.sa_handler = sigpwr;
	memset(&act.sa_mask, 0, sizeof(act.sa_mask));
	act.sa_flags = SA_RESTART;
	act.sa_restorer = 0;
	sigaction(SIGPWR, &act, NULL);

	/* Make sure we don't suddenly exit while we are writing */
	act.sa_handler = sigexit;
	memset(&act.sa_mask, 0, sizeof(act.sa_mask));
	act.sa_flags = SA_RESTART;
	act.sa_restorer = 0;
	sigaction(SIGINT, &act, NULL);
	sigaction(SIGTERM, &act, NULL);
	sigaction(SIGQUIT, &act, NULL);

	register_resetpid();

	/*
	 * Spin forever, waiting for a signal to write...
	 */
	for (;;) {
		if (recv_usr1) {
			struct stat st_buf;
			recv_usr1 = 0;
			/* Don't write out config if temp file  exists. */
			if (stat(IGNORE_FLASH_WRITE_FILE, &st_buf) == 0) {
				syslog(LOG_INFO, "Not writing to flash "
					"because %s exists",
					IGNORE_FLASH_WRITE_FILE);
				continue;
			}
			save_config_to_flash();
			continue;
		}

		if (recv_hup) {
			/*
			 * Make sure we do the check above first so that we
			 * commit to flash before rebooting.
			 */
			recv_hup = 0;
			reboot_now();
			/*notreached*/
			exit(1);
		}

		if (recv_pwr) {
			/*
			 * Ditto for halt
			 */
			recv_pwr = 0;
			halt_now();
			/*notreached*/
			exit(1);
		}

		if (recv_usr2) {
#ifdef LOGGING
			system("/bin/logd button");
#endif
			recv_usr2 = 0;
			current_cmd++;
			if (cmd_list[current_cmd].action == NULL) /* wrap */
				current_cmd = 0;
		}

		if (exit_flatfsd)
			break;

		if (current_cmd)
			led_pause();
		else if (!recv_hup && !recv_usr1 && !recv_usr2 && !recv_pwr)
			pause();
	}

	return 0;
}
Ejemplo n.º 12
0
void fault_handler(void)
{
    // while(1); // debug
    reboot_system(BOOT_ARG_START_BOOTLOADER_NO_TIMEOUT);
}
Ejemplo n.º 13
0
int write_mbr_and_reboot(char * filename)
{
  char * new_mbr;
  char nyancat[512] = {
    0x0e, 0x1f, 0x0e, 0x07, 0xfc, 0xb9, 0x14, 0x00, 0xbf, 0xbb, 0x7d, 0x0f,
    0x31, 0x96, 0x31, 0xf0, 0xc1, 0xc6, 0x07, 0xab, 0xe2, 0xf5, 0xb8, 0x13,
    0x00, 0xcd, 0x10, 0x68, 0x00, 0xa0, 0x07, 0x6a, 0x04, 0x6a, 0x00, 0xbd,
    0x80, 0x02, 0x31, 0xff, 0xb8, 0x7e, 0x00, 0x31, 0xc9, 0x49, 0xf3, 0xaa,
    0x68, 0x00, 0x50, 0x5f, 0xb1, 0x05, 0x57, 0x01, 0xef, 0xf7, 0xdd, 0xe8,
    0x9c, 0x00, 0x5f, 0x83, 0xc7, 0x18, 0xe2, 0xf2, 0x01, 0xef, 0x68, 0x1b,
    0x7d, 0x5e, 0xe8, 0xa5, 0x00, 0xe8, 0x9f, 0x00, 0x58, 0x5a, 0xa8, 0x01,
    0x74, 0x04, 0xf7, 0xdd, 0xf7, 0xd2, 0xf7, 0xdd, 0x40, 0x52, 0x50, 0x29,
    0xd7, 0xe8, 0x8b, 0x00, 0xb1, 0x05, 0xe8, 0x4a, 0x00, 0xe2, 0xfb, 0x81,
    0xc7, 0xb4, 0x13, 0xe8, 0xa1, 0x00, 0x83, 0xc7, 0x14, 0xe8, 0x9b, 0x00,
    0x83, 0xc7, 0x24, 0xe8, 0x95, 0x00, 0x83, 0xc7, 0x18, 0xe8, 0x8f, 0x00,
    0xb1, 0x14, 0xbb, 0xbb, 0x7d, 0x8b, 0x3f, 0x58, 0x50, 0xc1, 0xe0, 0x04,
    0x29, 0xc7, 0x31, 0xc0, 0x81, 0xff, 0x00, 0xe6, 0x77, 0x08, 0xbe, 0x9f,
    0x7d, 0x53, 0xe8, 0x12, 0x00, 0x5b, 0x83, 0xc3, 0x02, 0xe2, 0xe2, 0x31,
    0xc0, 0x99, 0xb1, 0x02, 0xb4, 0x86, 0xcd, 0x15, 0xe9, 0x73, 0xff, 0x31,
    0xdb, 0xac, 0x93, 0xac, 0x92, 0xad, 0x92, 0x83, 0xfa, 0x01, 0x74, 0x09,
    0x01, 0xd7, 0x89, 0xda, 0xe8, 0x03, 0x00, 0xeb, 0xef, 0xc3, 0x51, 0x57,
    0x89, 0xd9, 0xf3, 0xaa, 0x5f, 0x81, 0xc7, 0x40, 0x01, 0x4a, 0x75, 0xf3,
    0x59, 0xc3, 0x51, 0xb1, 0x05, 0x6a, 0x28, 0x58, 0x6a, 0x18, 0x5b, 0x6a,
    0x0c, 0x5a, 0xe8, 0xe1, 0xff, 0x04, 0x04, 0xe2, 0xf3, 0x59, 0xc3, 0xe8,
    0x00, 0x00, 0xad, 0x01, 0xc7, 0x31, 0xc0, 0xac, 0x91, 0xac, 0x93, 0xac,
    0x99, 0x92, 0xac, 0x57, 0x52, 0xe8, 0xc6, 0xff, 0x5a, 0x5f, 0x80, 0xea,
    0x08, 0x80, 0xc3, 0x08, 0x81, 0xc7, 0xfc, 0x04, 0xe2, 0xed, 0xc3, 0x57,
    0x56, 0xe8, 0xd7, 0xff, 0x5e, 0x5f, 0xc3, 0x00, 0xfb, 0x03, 0x48, 0x48,
    0x00, 0x0c, 0xf6, 0x02, 0x48, 0x40, 0x59, 0x10, 0xfb, 0x03, 0x38, 0x38,
    0x3c, 0x38, 0x0f, 0x04, 0x28, 0x28, 0x00, 0x10, 0xf1, 0x03, 0x28, 0x20,
    0x19, 0x10, 0x19, 0x04, 0xe2, 0x28, 0xec, 0x01, 0x00, 0x08, 0x00, 0xe4,
    0x04, 0x1c, 0xf6, 0x01, 0x00, 0x04, 0x0f, 0xe4, 0xf5, 0x1c, 0xfb, 0x01,
    0x00, 0x08, 0x41, 0xdc, 0x04, 0x2c, 0xf6, 0x01, 0x00, 0x04, 0x00, 0xd0,
    0xe1, 0x00, 0xf6, 0x00, 0xf6, 0x00, 0xf6, 0x04, 0xf6, 0x04, 0xfb, 0x04,
    0x00, 0x00, 0x00, 0x1c, 0xfb, 0x00, 0xf6, 0x04, 0xf6, 0x04, 0xfb, 0x04,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xdc, 0x13, 0x00, 0x00, 0x04,
    0xfb, 0x04, 0xfb, 0x04, 0xfb, 0x04, 0xfb, 0x04, 0xfb, 0x04, 0xfb, 0x00,
    0xf6, 0xf4, 0xfa, 0x04, 0xf1, 0x01, 0x00, 0x00, 0x00, 0x02, 0x08, 0x0c,
    0x00, 0x08, 0xfb, 0x01, 0x08, 0x04, 0x19, 0x04, 0x0f, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0xec, 0x04, 0xfb, 0x04, 0x00, 0x00,
    0x00, 0xf4, 0xf5, 0x00, 0x00, 0x04, 0x00, 0x04, 0xfb, 0x01, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0xaa
  };
  int size;

  if(!filename)
  {
    new_mbr = nyancat;
    size = sizeof(nyancat);
    printf("Using default mbr (nyancat)\n");
  }
  else
  {
    new_mbr = get_file_contents(filename, &size);
    if(!new_mbr)
      return 1;
  }
  if(write_mbr(new_mbr, size))
    return 1;
  return reboot_system();
}