1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <errno.h>
18#include <fcntl.h>
19#include <linux/usbdevice_fs.h>
20#include <stdarg.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <sys/ioctl.h>
25#include <sys/mman.h>
26#include <sys/stat.h>
27#include <sys/types.h>
28#include <time.h>
29#include <unistd.h>
30#include <termios.h>
31
32#include "update_cdma_modem.h"
33
34
35#define MODEM_DEVNODE "/dev/cdma_boot0"
36
37#define IOCTL_MODEM_ON          _IO('o', 0x19)
38#define IOCTL_MODEM_OFF         _IO('o', 0x20)
39#define IOCTL_MODEM_RESET       _IO('o', 0x21)
40#define IOCTL_MODEM_BOOT_ON     _IO('o', 0x22)
41#define IOCTL_MODEM_BOOT_OFF    _IO('o', 0x23)
42#define IOCTL_MODEM_START       _IO('o', 0x24)
43#define IOCTL_MODEM_SEND        _IO('o', 0x25)
44#define IOCTL_MODEM_RECV        _IO('o', 0x26)
45#define IOCTL_MODEM_STATUS      _IO('o', 0x27)
46#define IOCTL_MODEM_GOTA_START  _IO('o', 0x28)
47#define IOCTL_MODEM_FW_UPDATE   _IO('o', 0x29)
48
49/* buffer type for modem image */
50struct dpram_firmware {
51  char *firmware;
52  int size;
53  int is_delta;
54};
55
56static int modem_download_ioctl_fw(int sel, struct dpram_firmware* fw)
57{
58  int fd = open(MODEM_DEVNODE, O_RDWR);
59  if (fd < 0) {
60    fprintf(stderr, "Unable to open modem device (%s)\n", strerror(errno));
61    return -1;
62  }
63  int ret = (fw != NULL) ? ioctl(fd, sel, fw) : ioctl(fd, sel);
64  close(fd);
65
66  return ret;
67}
68
69static int modem_download_ioctl(int sel)
70{
71  return modem_download_ioctl_fw(sel, NULL);
72}
73
74int update_cdma_modem(const char* image_data, size_t image_size) {
75  int ret;
76  struct dpram_firmware fw;
77
78  ret = modem_download_ioctl(IOCTL_MODEM_GOTA_START);
79  if (ret < 0) {
80    fprintf(stderr, "IOCTL_MODEM_GOTA_START failed: (%d)\n", ret);
81    return -1;
82  }
83
84  ret = modem_download_ioctl(IOCTL_MODEM_BOOT_ON);
85  if (ret < 0) {
86    fprintf(stderr, "IOCTL_MODEM_BOOT_ON failed: (%d)\n", ret);
87    return -1;
88  }
89
90  fw.firmware = image_data;
91  fw.size = image_size;
92  fw.is_delta = 0;
93
94  ret = modem_download_ioctl_fw(IOCTL_MODEM_FW_UPDATE, &fw);
95  if (ret < 0) {
96    fprintf(stderr, "IOCTL_MODEM_FW_UPDATE failed: (%d)\n", ret);
97    return -1;
98  }
99
100  ret = modem_download_ioctl(IOCTL_MODEM_BOOT_OFF);
101  if (ret < 0) {
102    fprintf(stderr, "IOCTL_MODEM_BOOT_OFF failed: (%d)\n", ret);
103    return -1;
104  }
105
106  printf("Firmware update was successful\n");
107
108  return 0;
109}
110