Blame tools/ipod-scsi.c

Packit 29108b
/* Copyright (c) 2007, Christophe Fergeau  <teuf@gnome.org>
Packit 29108b
 * Part of the libgpod project.
Packit 29108b
 * 
Packit 29108b
 * URL: http://www.gtkpod.org/
Packit 29108b
 * URL: http://gtkpod.sourceforge.net/
Packit 29108b
 *
Packit 29108b
 * The code contained in this file is free software; you can redistribute
Packit 29108b
 * it and/or modify it under the terms of the GNU Lesser General Public
Packit 29108b
 * License as published by the Free Software Foundation; either version
Packit 29108b
 * 2.1 of the License, or (at your option) any later version.
Packit 29108b
 *
Packit 29108b
 * This file is distributed in the hope that it will be useful,
Packit 29108b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 29108b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 29108b
 * Lesser General Public License for more details.
Packit 29108b
 *
Packit 29108b
 * You should have received a copy of the GNU Lesser General Public
Packit 29108b
 * License along with this code; if not, write to the Free Software
Packit 29108b
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
Packit 29108b
 *
Packit 29108b
 * iTunes and iPod are trademarks of Apple
Packit 29108b
 *
Packit 29108b
 * This product is not supported/written/published by Apple!
Packit 29108b
 *
Packit 29108b
 */
Packit 29108b
#ifdef HAVE_CONFIG_H
Packit 29108b
#include <config.h>
Packit 29108b
#endif
Packit 29108b
Packit 29108b
#include <fcntl.h>
Packit 29108b
#include <stdio.h>
Packit 29108b
#include <stdlib.h>
Packit 29108b
#include <stdint.h>
Packit 29108b
#include <string.h>
Packit 29108b
#include <unistd.h>
Packit 29108b
#include <scsi/sg_cmds.h>
Packit 29108b
#include <sys/stat.h>
Packit 29108b
#include <time.h>
Packit 29108b
Packit 29108b
#include <glib.h>
Packit 29108b
Packit 29108b
extern void sync_time (const char *device, struct tm *tm);
Packit 29108b
extern char *read_sysinfo_extended (const char *device);
Packit 29108b
/* do_sg_inquiry and read_sysinfo_extended were heavily
Packit 29108b
 * inspired from libipoddevice
Packit 29108b
 */
Packit 29108b
static unsigned char
Packit 29108b
do_sg_inquiry (int fd, int page, char *buf, char **start)
Packit 29108b
{
Packit 29108b
    const unsigned int IPOD_BUF_LENGTH = 252;
Packit 29108b
Packit 29108b
    if (sg_ll_inquiry (fd, 0, 1, page, buf, IPOD_BUF_LENGTH, 1, 0) != 0) {
Packit 29108b
	*start = NULL;
Packit 29108b
	return 0;
Packit 29108b
    } else {
Packit 29108b
	*start = buf + 4;
Packit 29108b
	return (unsigned char)buf[3];
Packit 29108b
    }
Packit 29108b
}
Packit 29108b
Packit 29108b
G_GNUC_INTERNAL char *
Packit 29108b
read_sysinfo_extended (const char *device)
Packit 29108b
{
Packit 29108b
    int fd;
Packit 29108b
    const unsigned int IPOD_XML_PAGE = 0xc0;
Packit 29108b
    unsigned char len;
Packit 29108b
    char buf[512];
Packit 29108b
    char *start;
Packit 29108b
    char *offsets;
Packit 29108b
    GString *xml_sysinfo;
Packit 29108b
    unsigned int i;
Packit 29108b
Packit 29108b
    fd = open (device, O_RDONLY);
Packit 29108b
    if (fd < 0) {
Packit 29108b
	return NULL;
Packit 29108b
    }
Packit 29108b
Packit 29108b
    len = do_sg_inquiry (fd, IPOD_XML_PAGE, buf, &start;;
Packit 29108b
    if (start == NULL || len == 0) {
Packit 29108b
	close(fd);
Packit 29108b
	return NULL;
Packit 29108b
    }
Packit 29108b
Packit 29108b
    offsets = g_new (char, len+1);
Packit 29108b
    memcpy(offsets, start, len);
Packit 29108b
    offsets[len] = 0;
Packit 29108b
Packit 29108b
    xml_sysinfo = g_string_new_len (NULL, 512);
Packit 29108b
    if (xml_sysinfo == NULL) {
Packit 29108b
	g_free (offsets);
Packit 29108b
	close (fd);
Packit 29108b
	return NULL;
Packit 29108b
    }
Packit 29108b
Packit 29108b
    for (i = 0; offsets[i]; i++) {
Packit 29108b
	bzero(buf, 512);
Packit 29108b
	len = do_sg_inquiry (fd, offsets[i], buf, &start;;
Packit 29108b
	start[len] = 0;
Packit 29108b
	g_string_append (xml_sysinfo, start);
Packit 29108b
    }
Packit 29108b
    
Packit 29108b
    g_free (offsets);
Packit 29108b
    close (fd);
Packit 29108b
Packit 29108b
    return g_string_free (xml_sysinfo, FALSE);
Packit 29108b
}
Packit 29108b
Packit 29108b
static void do_sg_write_buffer (const char *device, void *buffer, size_t len)
Packit 29108b
{
Packit 29108b
    int fd;
Packit 29108b
    guint i;
Packit 29108b
Packit 29108b
    fd = open (device, O_RDWR);
Packit 29108b
    if (fd < 0) {
Packit 29108b
	g_warning ("Couldn't open device %s", device);
Packit 29108b
	return;
Packit 29108b
    }
Packit 29108b
Packit 29108b
    g_print ("    Data Payload: ");
Packit 29108b
    for (i = 0; i < len; i++) {
Packit 29108b
	g_print ("%02x ", *((guchar *)buffer+i));
Packit 29108b
    }
Packit 29108b
    g_print ("\n");
Packit 29108b
    if (sg_ll_write_buffer (fd, 1, 0, 0x0c0000, buffer, len, 1, 1) != 0) {
Packit 29108b
	g_print ("Error sending data\n");
Packit 29108b
    }
Packit 29108b
    close(fd);
Packit 29108b
}
Packit 29108b
Packit 29108b
G_GNUC_INTERNAL void sync_time (const char *device, struct tm *tm)
Packit 29108b
{
Packit 29108b
    struct iPodTime {
Packit 29108b
	guint16 year;
Packit 29108b
	guint16 days;
Packit 29108b
	guchar timezone;
Packit 29108b
	guchar hour;
Packit 29108b
	guchar minute;
Packit 29108b
	guchar second;
Packit 29108b
	guchar dst;
Packit 29108b
	guchar padding[3];
Packit 29108b
    } __attribute__((__packed__));
Packit 29108b
    struct iPodTime ipod_time;
Packit 29108b
Packit 29108b
    if (tm == NULL) {
Packit 29108b
	time_t current_time;
Packit 29108b
	current_time = time (NULL);
Packit 29108b
	tm = localtime (&current_time);
Packit 29108b
    }
Packit 29108b
Packit 29108b
    ipod_time.year = GUINT16_TO_BE (1900+tm->tm_year);
Packit 29108b
    ipod_time.days = GUINT16_TO_BE (tm->tm_yday);
Packit 29108b
    /* the timezone value is the shift east of UTC in 15 min chunks
Packit 29108b
     * so eg. UTC+2 is 2*4 = 8
Packit 29108b
     */
Packit 29108b
    ipod_time.timezone = tm->tm_gmtoff / 900;
Packit 29108b
    ipod_time.hour = tm->tm_hour;
Packit 29108b
    ipod_time.minute = tm->tm_min;
Packit 29108b
    ipod_time.second = tm->tm_sec;
Packit 29108b
    if (tm->tm_isdst) {
Packit 29108b
	ipod_time.dst = 1;
Packit 29108b
    } else {
Packit 29108b
	ipod_time.dst = 0;
Packit 29108b
    }
Packit 29108b
    memset (ipod_time.padding, 0, sizeof (ipod_time.padding));
Packit 29108b
Packit 29108b
    do_sg_write_buffer (device, &ipod_time, sizeof (ipod_time));
Packit 29108b
}