January 11, 2010

Parse HTML

Use this link to parse your code so that it can be pasted on blogger.

http://www.blogcrowds.com/resources/parse_html.php

Mini2440 download program on Linux

To download image to the mini2440 bootloader, the manual requires to install a Windows USB Driver, and a DNW2.exe program. I did not want to install some unknown EXE program on my computer. Fortunately, core_rui wrote a simple Linux program to do the whole trick. No driver needed. I simply LOVE the simplicity of this. You can run this program in a Vmware virtual machine (with USB 2.0 emulation enabled). Note this program uses libusb. So to compile it you need to install libusb-dev on your computer.


/* dnw2 linux main file. This depends on libusb.
*
* Author: Fox <hulifox008@163.com>
* License: GPL
*
*/



#include <stdio.h>
#include <usb.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#define QQ2440_SECBULK_IDVENDOR 0x5345
#define QQ2440_SECBULK_IDPRODUCT 0x1234


struct usb_dev_handle * open_port()
{
struct usb_bus *busses, *bus;

usb_init();
usb_find_busses();
usb_find_devices();

busses = usb_get_busses();
for(bus=busses;bus;bus=bus->next)
{
struct usb_device *dev;
for(dev=bus->devices;dev;dev=dev->next)
{
if( QQ2440_SECBULK_IDVENDOR==dev->descriptor.idVendor
&& QQ2440_SECBULK_IDPRODUCT==dev->descriptor.idProduct)
{
printf("Target usb device found!n");
struct usb_dev_handle *hdev = usb_open(dev);
if(!hdev)
{
perror("Cannot open device");
}
else
{
if(0!=usb_claim_interface(hdev, 0))
{
perror("Cannot claim interface");
usb_close(hdev);
hdev = NULL;
}
}
return hdev;
}
}
}

printf("Target usb device not found!n");

return NULL;
}

void usage()
{
printf("Usage: dnw2 <file>nn");
}

unsigned char* prepare_write_buf(char *filename, unsigned int *len)
{
unsigned char *write_buf = NULL;
struct stat fs;

int fd = open(filename, O_RDONLY);
if(-1==fd)
{
perror("Cannot open file");
return NULL;
}
if(-1==fstat(fd, &fs))
{
perror("Cannot get file size");
goto error;
}
write_buf = (unsigned char*)malloc(fs.st_size+10);
if(NULL==write_buf)
{
perror("malloc failed");
goto error;
}

if(fs.st_size != read(fd, write_buf+8, fs.st_size))
{
perror("Reading file failed");
goto error;
}

printf("Filename : %sn", filename);
printf("Filesize : %d bytesn", fs.st_size);

*((u_int32_t*)write_buf) = 0x30000000; //download address
*((u_int32_t*)write_buf+1) = fs.st_size + 10; //download size;

*len = fs.st_size + 10;
return write_buf;

error:
if(fd!=-1) close(fd);
if(NULL!=write_buf) free(write_buf);
fs.st_size = 0;
return NULL;

}

int main(int argc, char *argv[])
{
if(2!=argc)
{
usage();
return 1;
}

struct usb_dev_handle *hdev = open_port();
if(!hdev)
{
return 1;
}

unsigned int len = 0;
unsigned char* write_buf = prepare_write_buf(argv[1], &len);
if(NULL==write_buf) return 1;

unsigned int remain = len;
unsigned int towrite;
printf("Writing data ...n");
while(remain)
{
towrite = remain>512 ? 512 : remain;
if(towrite != usb_bulk_write(hdev, 0x03, write_buf+(len-remain), towrite, 3000))
{
perror("usb_bulk_write failed");
break;
}
remain-=towrite;
printf("r%d%t %d bytes ", (len-remain)*100/len, len-remain);
fflush(stdout);
}
if(0==remain) printf("Done!n");
return 0;
}


To compile: gcc dnw2.c -o dnw2 -lusb

January 8, 2010

Troubleshooting: X connection to localhost:10.0 broken (explicit kill or server shutdown).

If you remote connect to UNIX from Windows, then it's likely you will bump into this error message at least once in your lifetime:

X connection to localhost:10.0 broken (explicit kill or server shutdown).

This error messages occurs when you try to execute an X Window graphics user interface software, but you haven't started the X Windows display software on your Windows machine.

The X Window display server that I prefer to use on Windows is Xming 6.9.0.31. It's available on SourceForge and it's low-maintenance. If you haven't gotten a X Window display server software, you might give it a try. After starting Xming, you should not have this problem again.

However, if this problem does occur when you have Xming running, it's probably because you did not enable X11 forwarding in PuTTY, or your ssh software.

Chieh Cheng
Sun, 07 Jun 2009 18:35:41 +0000

Checklist for using Xming through a tunneled Putty session.

Putty:
* In the Connection->SSH->X11 panel, set the Enable X11 forwarding checkbox.
* (optional) In the same panel, enter "localhost:0" as the X display location. This redirects forwarded X traffic to display number 0. Match the Xming Display number, set below.
* Generally, MIT-Magic-Cookie-1 (set by default) is the most common authentication protocol. This will be added/updated in your .Xauthority file after a successful login for each display (one per Putty session).

Xming:
* In the Select display settings panel, set the Display number to 0. The IP port number used is 6000 + this number, so anything other than 0 will affect the display number used by X clients.
* In the same panel, if you want to connect directly from the remote client to the Xming server (not through Putty's X Forwarding), you should probably check the No Access Control box or your connection will be refused.

Remote system:
* Verify your SSH configuration allows X11 forwarding. In OpenSSH, the .ssh/config file should contain the entry "ForwardX11 yes".
* Your DISPLAY environment variable will be set automatically by Putty upon successful login. This will be "localhost:10.0" for the first session when the remote system's SSH daemon X11DisplayOffset is set to 10 (default). This display number will correspond to a TCP port that is listening on 6000 + display number, e.g. 6010. Use the command "netstat -an | grep -i listen" to see the ports on a Unix/Linux/Solaris box.
* If you "su" or "sudo" to a different user after login, you must merge the .Xauthority entry for the display associated with your session into the .Xauthority file of the new user. The DISPLAY environment variable should be set and exported the same as for the login user, for convenience. Otherwise, the X applications you lanuch with the new user will not have the authority to connect to your X server.

Windows:

* For the "localhost" part of the X display location to be converted to the correct IP address, your hosts file (%SYSTEMROOT%\System32\drivers\etc\hosts) must contain the entry "127.0.0.1 localhost".

* IMPORTANT: If you are using Windows Vista, the hosts file does not contain this entry by default. Instead, there is a "::1 localhost", which is for IPv6 instead of IPv4. Comment out/delete that entry and add the entry " 127.0.0.1 localhost". This causes the "X connection to localhost:10.0 broken (explicit kill or server shutdown)" error to appear when you try to launch an X client application.

* If you use a firewall, make sure that Putty SSH is allowed through it. If you can't establish a login session with Putty, X applications aren't going to work, either.