2008-06-27 22:48:25 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
*
|
|
|
|
* Copyright (C) 2007 by Dominik Wenger
|
2008-06-30 22:09:45 +00:00
|
|
|
* $Id$
|
2008-06-27 22:48:25 +00:00
|
|
|
*
|
|
|
|
* 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 "detect.h"
|
|
|
|
|
|
|
|
#include <QtCore>
|
|
|
|
#include <QDebug>
|
2008-10-24 22:31:07 +00:00
|
|
|
|
2008-06-27 22:48:25 +00:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
// Windows Includes
|
|
|
|
#if defined(Q_OS_WIN32)
|
|
|
|
#if defined(UNICODE)
|
|
|
|
#define _UNICODE
|
|
|
|
#endif
|
|
|
|
#include <windows.h>
|
|
|
|
#include <tchar.h>
|
|
|
|
#include <lm.h>
|
|
|
|
#include <windows.h>
|
|
|
|
#include <setupapi.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Linux and Mac includes
|
|
|
|
#if defined(Q_OS_LINUX) || defined(Q_OS_MACX)
|
|
|
|
#include <usb.h>
|
|
|
|
#include <sys/utsname.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Linux includes
|
|
|
|
#if defined(Q_OS_LINUX)
|
|
|
|
#include <mntent.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Mac includes
|
|
|
|
#if defined(Q_OS_MACX)
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/ucred.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#endif
|
|
|
|
|
2009-03-21 16:30:40 +00:00
|
|
|
#include "utils.h"
|
2008-06-27 22:48:25 +00:00
|
|
|
|
|
|
|
/** @brief detect permission of user (only Windows at moment).
|
|
|
|
* @return enum userlevel.
|
|
|
|
*/
|
|
|
|
#if defined(Q_OS_WIN32)
|
2008-06-27 23:19:59 +00:00
|
|
|
enum Detect::userlevel Detect::userPermissions(void)
|
2008-06-27 22:48:25 +00:00
|
|
|
{
|
|
|
|
LPUSER_INFO_1 buf;
|
|
|
|
NET_API_STATUS napistatus;
|
|
|
|
wchar_t userbuf[UNLEN];
|
|
|
|
DWORD usersize = UNLEN;
|
|
|
|
BOOL status;
|
|
|
|
enum userlevel result;
|
2008-10-24 22:31:07 +00:00
|
|
|
|
2008-06-27 22:48:25 +00:00
|
|
|
status = GetUserNameW(userbuf, &usersize);
|
|
|
|
if(!status)
|
|
|
|
return ERR;
|
2008-10-24 22:31:07 +00:00
|
|
|
|
2008-06-27 22:48:25 +00:00
|
|
|
napistatus = NetUserGetInfo(NULL, userbuf, (DWORD)1, (LPBYTE*)&buf);
|
|
|
|
|
|
|
|
switch(buf->usri1_priv) {
|
|
|
|
case USER_PRIV_GUEST:
|
|
|
|
result = GUEST;
|
|
|
|
break;
|
|
|
|
case USER_PRIV_USER:
|
|
|
|
result = USER;
|
|
|
|
break;
|
|
|
|
case USER_PRIV_ADMIN:
|
|
|
|
result = ADMIN;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
result = ERR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
NetApiBufferFree(buf);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @brief detects user permissions (only Windows at moment).
|
|
|
|
* @return a user readable string with the permission.
|
|
|
|
*/
|
|
|
|
QString Detect::userPermissionsString(void)
|
|
|
|
{
|
|
|
|
QString result;
|
2008-06-27 23:19:59 +00:00
|
|
|
int perm = userPermissions();
|
2008-06-27 22:48:25 +00:00
|
|
|
switch(perm) {
|
|
|
|
case GUEST:
|
|
|
|
result = QObject::tr("Guest");
|
|
|
|
break;
|
|
|
|
case ADMIN:
|
|
|
|
result = QObject::tr("Admin");
|
|
|
|
break;
|
|
|
|
case USER:
|
|
|
|
result = QObject::tr("User");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
result = QObject::tr("Error");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
#endif
|
2008-10-24 22:31:07 +00:00
|
|
|
|
2008-06-27 22:48:25 +00:00
|
|
|
|
|
|
|
/** @brief detects current Username.
|
|
|
|
* @return string with Username.
|
|
|
|
*/
|
|
|
|
QString Detect::userName(void)
|
|
|
|
{
|
|
|
|
#if defined(Q_OS_WIN32)
|
|
|
|
wchar_t userbuf[UNLEN];
|
|
|
|
DWORD usersize = UNLEN;
|
|
|
|
BOOL status;
|
2008-10-24 22:31:07 +00:00
|
|
|
|
2008-06-27 22:48:25 +00:00
|
|
|
status = GetUserNameW(userbuf, &usersize);
|
|
|
|
|
|
|
|
return QString::fromWCharArray(userbuf);
|
|
|
|
#endif
|
|
|
|
#if defined(Q_OS_LINUX) || defined(Q_OS_MACX)
|
|
|
|
struct passwd *user;
|
|
|
|
user = getpwuid(geteuid());
|
|
|
|
return QString(user->pw_name);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** @brief detects the OS Version
|
|
|
|
* @return String with OS Version.
|
|
|
|
*/
|
|
|
|
QString Detect::osVersionString(void)
|
|
|
|
{
|
|
|
|
QString result;
|
|
|
|
#if defined(Q_OS_WIN32)
|
|
|
|
OSVERSIONINFO osvi;
|
|
|
|
ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
|
|
|
|
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
|
|
|
|
GetVersionEx(&osvi);
|
|
|
|
|
|
|
|
result = QString("Windows version %1.%2, ").arg(osvi.dwMajorVersion).arg(osvi.dwMinorVersion);
|
2008-07-04 20:26:36 +00:00
|
|
|
if(osvi.szCSDVersion)
|
|
|
|
result += QString("build %1 (%2)").arg(osvi.dwBuildNumber)
|
|
|
|
.arg(QString::fromWCharArray(osvi.szCSDVersion));
|
|
|
|
else
|
|
|
|
result += QString("build %1").arg(osvi.dwBuildNumber);
|
2008-06-27 22:48:25 +00:00
|
|
|
#endif
|
|
|
|
#if defined(Q_OS_LINUX) || defined(Q_OS_MACX)
|
|
|
|
struct utsname u;
|
|
|
|
int ret;
|
|
|
|
ret = uname(&u);
|
|
|
|
|
|
|
|
result = QString("CPU: %1<br/>System: %2<br/>Release: %3<br/>Version: %4")
|
|
|
|
.arg(u.machine).arg(u.sysname).arg(u.release).arg(u.version);
|
|
|
|
#endif
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2008-06-28 18:06:55 +00:00
|
|
|
QList<uint32_t> Detect::listUsbIds(void)
|
|
|
|
{
|
|
|
|
return listUsbDevices().keys();
|
|
|
|
}
|
|
|
|
|
2008-06-27 22:48:25 +00:00
|
|
|
/** @brief detect devices based on usb pid / vid.
|
|
|
|
* @return list with usb VID / PID values.
|
|
|
|
*/
|
2008-06-28 18:06:55 +00:00
|
|
|
QMap<uint32_t, QString> Detect::listUsbDevices(void)
|
2008-06-27 22:48:25 +00:00
|
|
|
{
|
2008-06-28 18:06:55 +00:00
|
|
|
QMap<uint32_t, QString> usbids;
|
2008-06-27 22:48:25 +00:00
|
|
|
// usb pid detection
|
2008-07-02 17:48:50 +00:00
|
|
|
#if defined(Q_OS_LINUX) || defined(Q_OS_MACX)
|
2008-06-27 22:48:25 +00:00
|
|
|
usb_init();
|
|
|
|
usb_find_busses();
|
|
|
|
usb_find_devices();
|
|
|
|
struct usb_bus *b;
|
|
|
|
b = usb_busses;
|
|
|
|
|
|
|
|
while(b) {
|
|
|
|
qDebug() << "bus:" << b->dirname << b->devices;
|
|
|
|
if(b->devices) {
|
|
|
|
qDebug() << "devices present.";
|
|
|
|
struct usb_device *u;
|
|
|
|
u = b->devices;
|
|
|
|
while(u) {
|
|
|
|
uint32_t id;
|
|
|
|
id = u->descriptor.idVendor << 16 | u->descriptor.idProduct;
|
2008-07-02 17:48:50 +00:00
|
|
|
// get identification strings
|
|
|
|
usb_dev_handle *dev;
|
|
|
|
QString name;
|
|
|
|
char string[256];
|
|
|
|
int res;
|
|
|
|
dev = usb_open(u);
|
|
|
|
if(dev) {
|
|
|
|
if(u->descriptor.iManufacturer) {
|
|
|
|
res = usb_get_string_simple(dev, u->descriptor.iManufacturer, string, sizeof(string));
|
|
|
|
if(res > 0)
|
|
|
|
name += QString::fromAscii(string) + " ";
|
|
|
|
}
|
|
|
|
if(u->descriptor.iProduct) {
|
|
|
|
res = usb_get_string_simple(dev, u->descriptor.iProduct, string, sizeof(string));
|
|
|
|
if(res > 0)
|
|
|
|
name += QString::fromAscii(string);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
usb_close(dev);
|
|
|
|
if(name.isEmpty()) name = QObject::tr("(no description available)");
|
|
|
|
|
|
|
|
if(id) usbids.insert(id, name);
|
2008-06-27 22:48:25 +00:00
|
|
|
u = u->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
b = b->next;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(Q_OS_WIN32)
|
|
|
|
HDEVINFO deviceInfo;
|
|
|
|
SP_DEVINFO_DATA infoData;
|
|
|
|
DWORD i;
|
|
|
|
|
|
|
|
// Iterate over all devices
|
|
|
|
// by doing it this way it's unneccessary to use GUIDs which might be not
|
|
|
|
// present in current MinGW. It also seemed to be more reliably than using
|
|
|
|
// a GUID.
|
|
|
|
// See KB259695 for an example.
|
|
|
|
deviceInfo = SetupDiGetClassDevs(NULL, NULL, NULL, DIGCF_ALLCLASSES | DIGCF_PRESENT);
|
|
|
|
|
|
|
|
infoData.cbSize = sizeof(SP_DEVINFO_DATA);
|
|
|
|
|
|
|
|
for(i = 0; SetupDiEnumDeviceInfo(deviceInfo, i, &infoData); i++) {
|
|
|
|
DWORD data;
|
|
|
|
LPTSTR buffer = NULL;
|
|
|
|
DWORD buffersize = 0;
|
2008-06-28 18:06:55 +00:00
|
|
|
QString description;
|
2008-06-27 22:48:25 +00:00
|
|
|
|
|
|
|
// get device desriptor first
|
|
|
|
// for some reason not doing so results in bad things (tm)
|
|
|
|
while(!SetupDiGetDeviceRegistryProperty(deviceInfo, &infoData,
|
|
|
|
SPDRP_DEVICEDESC,&data, (PBYTE)buffer, buffersize, &buffersize)) {
|
|
|
|
if(GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
|
|
|
|
if(buffer) free(buffer);
|
|
|
|
// double buffer size to avoid problems as per KB888609
|
|
|
|
buffer = (LPTSTR)malloc(buffersize * 2);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// now get the hardware id, which contains PID and VID.
|
2008-06-28 18:06:55 +00:00
|
|
|
while(!SetupDiGetDeviceRegistryProperty(deviceInfo, &infoData,
|
|
|
|
SPDRP_LOCATION_INFORMATION,&data, (PBYTE)buffer, buffersize, &buffersize)) {
|
|
|
|
if(GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
|
|
|
|
if(buffer) free(buffer);
|
|
|
|
// double buffer size to avoid problems as per KB888609
|
|
|
|
buffer = (LPTSTR)malloc(buffersize * 2);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
description = QString::fromWCharArray(buffer);
|
|
|
|
|
2008-06-27 22:48:25 +00:00
|
|
|
while(!SetupDiGetDeviceRegistryProperty(deviceInfo, &infoData,
|
|
|
|
SPDRP_HARDWAREID,&data, (PBYTE)buffer, buffersize, &buffersize)) {
|
|
|
|
if(GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
|
|
|
|
if(buffer) free(buffer);
|
|
|
|
// double buffer size to avoid problems as per KB888609
|
|
|
|
buffer = (LPTSTR)malloc(buffersize * 2);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int vid, pid, rev;
|
2008-06-29 09:13:58 +00:00
|
|
|
if(_stscanf(buffer, _TEXT("USB\\Vid_%x&Pid_%x&Rev_%x"), &vid, &pid, &rev) == 3) {
|
2008-06-27 22:48:25 +00:00
|
|
|
uint32_t id;
|
|
|
|
id = vid << 16 | pid;
|
2008-06-28 18:06:55 +00:00
|
|
|
usbids.insert(id, description);
|
2008-06-29 09:13:58 +00:00
|
|
|
qDebug("VID: %04x, PID: %04x", vid, pid);
|
2008-06-27 22:48:25 +00:00
|
|
|
}
|
|
|
|
if(buffer) free(buffer);
|
|
|
|
}
|
|
|
|
SetupDiDestroyDeviceInfoList(deviceInfo);
|
|
|
|
|
|
|
|
#endif
|
|
|
|
return usbids;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** @brief detects current system proxy
|
|
|
|
* @return QUrl with proxy or empty
|
|
|
|
*/
|
|
|
|
QUrl Detect::systemProxy(void)
|
|
|
|
{
|
|
|
|
#if defined(Q_OS_LINUX)
|
|
|
|
return QUrl(getenv("http_proxy"));
|
|
|
|
#elif defined(Q_OS_WIN32)
|
|
|
|
HKEY hk;
|
|
|
|
wchar_t proxyval[80];
|
|
|
|
DWORD buflen = 80;
|
|
|
|
long ret;
|
|
|
|
DWORD enable;
|
|
|
|
DWORD enalen = sizeof(DWORD);
|
|
|
|
|
|
|
|
ret = RegOpenKeyEx(HKEY_CURRENT_USER,
|
|
|
|
_TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings"),
|
|
|
|
0, KEY_QUERY_VALUE, &hk);
|
|
|
|
if(ret != ERROR_SUCCESS) return QUrl("");
|
|
|
|
|
|
|
|
ret = RegQueryValueEx(hk, _TEXT("ProxyServer"), NULL, NULL, (LPBYTE)proxyval, &buflen);
|
|
|
|
if(ret != ERROR_SUCCESS) return QUrl("");
|
2008-10-24 22:31:07 +00:00
|
|
|
|
2008-06-27 22:48:25 +00:00
|
|
|
ret = RegQueryValueEx(hk, _TEXT("ProxyEnable"), NULL, NULL, (LPBYTE)&enable, &enalen);
|
|
|
|
if(ret != ERROR_SUCCESS) return QUrl("");
|
|
|
|
|
|
|
|
RegCloseKey(hk);
|
|
|
|
|
|
|
|
//qDebug() << QString::fromWCharArray(proxyval) << QString("%1").arg(enable);
|
|
|
|
if(enable != 0)
|
|
|
|
return QUrl("http://" + QString::fromWCharArray(proxyval));
|
|
|
|
else
|
2008-10-24 22:31:07 +00:00
|
|
|
return QUrl("");
|
2008-06-27 22:48:25 +00:00
|
|
|
#else
|
2008-10-24 22:31:07 +00:00
|
|
|
return QUrl("");
|
2008-06-27 22:48:25 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** @brief detects the installed Rockbox version
|
|
|
|
* @return QString with version. Empty if not aviable
|
|
|
|
*/
|
|
|
|
QString Detect::installedVersion(QString mountpoint)
|
|
|
|
{
|
2009-03-21 16:30:40 +00:00
|
|
|
RockboxInfo info(mountpoint);
|
|
|
|
if(!info.open())
|
2008-06-27 22:48:25 +00:00
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
2008-10-24 22:31:07 +00:00
|
|
|
|
2009-03-21 16:30:40 +00:00
|
|
|
return info.version();
|
2008-06-27 22:48:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-14 23:34:40 +00:00
|
|
|
/** @brief detects installed rockbox target string
|
|
|
|
* @return target name (platform) of installed Rockbox, empty string on error.
|
2008-06-27 22:48:25 +00:00
|
|
|
*/
|
2009-03-14 23:34:40 +00:00
|
|
|
QString Detect::installedTarget(QString mountpoint)
|
2008-06-27 22:48:25 +00:00
|
|
|
{
|
2009-03-21 16:30:40 +00:00
|
|
|
RockboxInfo info(mountpoint);
|
|
|
|
if(!info.open())
|
2008-06-27 22:48:25 +00:00
|
|
|
{
|
2009-03-14 23:34:40 +00:00
|
|
|
return "";
|
2008-06-27 22:48:25 +00:00
|
|
|
}
|
2008-10-24 22:31:07 +00:00
|
|
|
|
2009-03-21 16:30:40 +00:00
|
|
|
return info.target();
|
2008-06-27 22:48:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** @brief checks different Enviroment things. Ask if user wants to continue.
|
|
|
|
* @param settings A pointer to rbutils settings class
|
|
|
|
* @param permission if it should check for permission
|
|
|
|
* @param targetId the targetID to check for. if it is -1 no check is done.
|
2008-10-24 22:31:07 +00:00
|
|
|
* @return string with error messages if problems occurred, empty strings if none.
|
2008-06-27 22:48:25 +00:00
|
|
|
*/
|
2009-03-14 23:34:40 +00:00
|
|
|
QString Detect::check(RbSettings* settings, bool permission)
|
2008-06-27 22:48:25 +00:00
|
|
|
{
|
|
|
|
QString text = "";
|
|
|
|
|
|
|
|
// check permission
|
|
|
|
if(permission)
|
|
|
|
{
|
|
|
|
#if defined(Q_OS_WIN32)
|
|
|
|
if(Detect::userPermissions() != Detect::ADMIN)
|
|
|
|
{
|
2008-10-24 22:31:07 +00:00
|
|
|
text += QObject::tr("<li>Permissions insufficient for bootloader "
|
|
|
|
"installation.\nAdministrator priviledges are necessary.</li>");
|
|
|
|
}
|
2008-06-27 22:48:25 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check TargetId
|
2009-03-14 23:34:40 +00:00
|
|
|
QString installed = installedTarget(settings->mountpoint());
|
|
|
|
if(!installed.isEmpty() && installed != settings->curPlatform())
|
2008-10-24 22:31:07 +00:00
|
|
|
{
|
2009-03-14 23:34:40 +00:00
|
|
|
text += QObject::tr("<li>Target mismatch detected.\n"
|
|
|
|
"Installed target: %1, selected target: %2.</li>")
|
|
|
|
.arg(settings->name(installed), settings->curName());
|
2008-06-27 22:48:25 +00:00
|
|
|
}
|
|
|
|
|
2008-10-24 22:31:07 +00:00
|
|
|
if(!text.isEmpty())
|
|
|
|
return QObject::tr("Problem detected:") + "<ul>" + text + "</ul>";
|
|
|
|
else
|
|
|
|
return text;
|
2008-06-27 22:48:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|