All pastes #688368 Raw Edit

Untitled

public text v1 · immutable
#688368 ·published 2007-09-09 10:45 UTC
rendered paste body
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id: crt0-pp-bl.S 11815 2006-12-19 11:33:53Z barrywardell $
 *
 * Copyright (C) 2007 by Jonathan Gordon
 *
 * All files in this archive are subject to the GNU General Public License.
 * See the file COPYING in the source tree root for full license agreement.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <ctype.h>
#include <stdbool.h>
#include "bootimg.h"

#include <usb.h> /* This tool requires libusb */


#define E200_INTERFACE 0
#define E200_ENDPOINT  1

#define E200_TIMEOUT    5000
#define PROGRAM_TIMEOUT 120000

#define SANDISK_VENDOR_ID  0x0781
#define SANDISK_PRODUCT_ID 0x0720
#define PP_VENDOR_ID       0x0b70
#define PP_PRODUCT_ID      0x0003

void put_le32(void * ptr, uint32_t val)
{
    uint8_t * b = ptr;
    
    b[0] = val;
    b[1] = val >> 8;
    b[2] = val >> 16;
    b[3] = val >> 24;
}


int send_dev(usb_dev_handle * ud, const void * data, uint32_t len)
{
    char * buf = (char *)data;
    int32_t todo;
    int ret;

    while (len > 0) 
    {
        todo = len > BUFSIZE ? BUFSIZE : len;

        ret = usb_bulk_write(ud, E200_ENDPOINT, buf, todo, E200_TIMEOUT);

        if (ret < 0)
        {
            fprintf(stderr, "\nBulk write error (%d, %s)\n", ret, strerror(-ret));
            return ret;
        }

        buf += ret;
        len -= ret;
    }
    return 0;
}

usb_dev_handle *device_get(int vendor, int product)
{
    struct usb_bus * bus, * busses;
    struct usb_device * dev;
    usb_dev_handle * ud;
    int ret, retries = 10;
    bool found = false;
    
    fprintf(stderr, "Searching for device %04x:%04x ... ", vendor, product);

 retry:
    found = false;
    usb_find_busses();
    usb_find_devices();

    busses = usb_get_busses();

    for (bus = busses; !found && bus; bus = bus->next)
    {
        for (dev = bus->devices; dev; dev = dev->next) 
        {
            if ((dev->descriptor.idVendor        == vendor  &&
                 dev->descriptor.idProduct       == product &&
                 dev->descriptor.bDeviceClass    == 0xff    &&
                 dev->descriptor.bDeviceSubClass == 0xff    &&
                 dev->descriptor.bDeviceProtocol == 0xff))
            {
                found = true;
                break;
            }
        }
        if (found)
        {
            fprintf(stderr, "found!\n");
            ud = usb_open(dev);
            if (!ud) 
            {
                fprintf(stderr, "Failed to open the device!\n");
                return NULL;
            }
            ret = usb_claim_interface(ud, E200_INTERFACE);

            if (ret < 0) 
            {
                fprintf(stderr, "Failed to claim the interface (%d, %s)\n",
                        ret, strerror(-ret));
                usb_close(ud);
                return NULL;
            }
            return ud;
        }
    }

    if (retries-- > 0) 
    {
        do_delay(1);
        fprintf(stderr, "%d ", retries);
        goto retry;
    }
    fprintf(stderr, "not found!\n");
    return NULL;
}

int main(int argc, char * argv[])
{
    (void)argc;(void)argv;
    usb_dev_handle * ud;
    int ret;
    char buffer[16];
    fprintf(stderr, "e200r-installer, (c) Jonathan Gordon 2007\n%s\n", "$Revision$");
    usb_init();
    ud = device_get(SANDISK_VENDOR_ID, SANDISK_PRODUCT_ID);
    if (!ud)
    {
        ud = device_get(PP_VENDOR_ID, PP_PRODUCT_ID);
        if (!ud)
            return 1;
    }
    fprintf(stderr, "Transferring Installer to Sansa...\n");
    put_le32(buffer, LEN_bootimg);
    ret = usb_bulk_write(ud, E200_ENDPOINT, buffer, sizeof LEN_bootimg, E200_TIMEOUT);
    if (ret < 0) 
    {
        fprintf(stderr, "\nLength write error (%d, %s)\n", ret, strerror(-ret));
        return 1;
    }
    ret = send_dev(ud, bootimg, LEN_bootimg);
    fprintf(stderr, "%d bytes LEN_bootimg\n", ret);

    usb_release_interface(ud, E200_INTERFACE);
    usb_close(ud);
    return 0;
}