diff options
Diffstat (limited to 'toolbox')
-rw-r--r-- | toolbox/Android.mk | 3 | ||||
-rw-r--r-- | toolbox/cmp.c | 1 | ||||
-rw-r--r-- | toolbox/date.c | 2 | ||||
-rw-r--r-- | toolbox/dd.h | 2 | ||||
-rw-r--r-- | toolbox/dynarray.c | 103 | ||||
-rw-r--r-- | toolbox/dynarray.h | 80 | ||||
-rw-r--r-- | toolbox/getprop.c | 29 | ||||
-rw-r--r-- | toolbox/hd.c | 1 | ||||
-rw-r--r-- | toolbox/ioctl.c | 3 | ||||
-rw-r--r-- | toolbox/ls.c | 152 | ||||
-rw-r--r-- | toolbox/lsof.c | 41 | ||||
-rw-r--r-- | toolbox/lsusb.c | 227 | ||||
-rw-r--r-- | toolbox/mount.c | 54 | ||||
-rw-r--r-- | toolbox/nandread.c | 2 | ||||
-rw-r--r-- | toolbox/notify.c | 1 | ||||
-rw-r--r-- | toolbox/ps.c | 14 | ||||
-rw-r--r-- | toolbox/r.c | 65 | ||||
-rw-r--r-- | toolbox/reboot.c | 14 | ||||
-rw-r--r-- | toolbox/schedtop.c | 3 | ||||
-rw-r--r-- | toolbox/setconsole.c | 2 | ||||
-rw-r--r-- | toolbox/top.c | 14 | ||||
-rw-r--r-- | toolbox/uptime.c | 10 | ||||
-rw-r--r-- | toolbox/wipe.c | 4 |
23 files changed, 627 insertions, 200 deletions
diff --git a/toolbox/Android.mk b/toolbox/Android.mk index ef3980a..ff01172 100644 --- a/toolbox/Android.mk +++ b/toolbox/Android.mk @@ -57,10 +57,11 @@ TOOLS := \ lsof LOCAL_SRC_FILES:= \ + dynarray.c \ toolbox.c \ $(patsubst %,%.c,$(TOOLS)) -LOCAL_SHARED_LIBRARIES := libcutils libc +LOCAL_SHARED_LIBRARIES := libcutils libc libusbhost LOCAL_MODULE:= toolbox diff --git a/toolbox/cmp.c b/toolbox/cmp.c index 9bd2e19..80635ad 100644 --- a/toolbox/cmp.c +++ b/toolbox/cmp.c @@ -2,6 +2,7 @@ #include <stdlib.h> #include <string.h> #include <stdint.h> +#include <unistd.h> #include <fcntl.h> #include <sys/ioctl.h> #include <errno.h> diff --git a/toolbox/date.c b/toolbox/date.c index 13b5210..35ef846 100644 --- a/toolbox/date.c +++ b/toolbox/date.c @@ -1,10 +1,12 @@ #include <stdio.h> #include <stdlib.h> +#include <unistd.h> #include <fcntl.h> #include <string.h> #include <errno.h> #include <time.h> #include <linux/android_alarm.h> +#include <sys/ioctl.h> static void settime(char *s) { struct tm tm; diff --git a/toolbox/dd.h b/toolbox/dd.h index 794a464..cca1024 100644 --- a/toolbox/dd.h +++ b/toolbox/dd.h @@ -35,6 +35,8 @@ * @(#)dd.h 8.3 (Berkeley) 4/2/94 */ +#include <stdint.h> + /* Input/output stream state. */ typedef struct { u_char *db; /* buffer address */ diff --git a/toolbox/dynarray.c b/toolbox/dynarray.c new file mode 100644 index 0000000..e9b7b03 --- /dev/null +++ b/toolbox/dynarray.c @@ -0,0 +1,103 @@ +#include "dynarray.h" +#include <stdlib.h> +#include <limits.h> + +void +dynarray_init( dynarray_t *a ) +{ + a->count = a->capacity = 0; + a->items = NULL; +} + + +static void +dynarray_reserve_more( dynarray_t *a, int count ) +{ + int old_cap = a->capacity; + int new_cap = old_cap; + const int max_cap = INT_MAX/sizeof(void*); + void** new_items; + int new_count = a->count + count; + + if (count <= 0) + return; + + if (count > max_cap - a->count) + abort(); + + new_count = a->count + count; + + while (new_cap < new_count) { + old_cap = new_cap; + new_cap += (new_cap >> 2) + 4; + if (new_cap < old_cap || new_cap > max_cap) { + new_cap = max_cap; + } + } + new_items = realloc(a->items, new_cap*sizeof(void*)); + if (new_items == NULL) + abort(); + + a->items = new_items; + a->capacity = new_cap; +} + +void +dynarray_append( dynarray_t *a, void* item ) +{ + if (a->count >= a->capacity) + dynarray_reserve_more(a, 1); + + a->items[a->count++] = item; +} + +void +dynarray_done( dynarray_t *a ) +{ + free(a->items); + a->items = NULL; + a->count = a->capacity = 0; +} + +// string arrays + +void strlist_init( strlist_t *list ) +{ + dynarray_init(list); +} + +void strlist_append_b( strlist_t *list, const void* str, size_t slen ) +{ + char *copy = malloc(slen+1); + memcpy(copy, str, slen); + copy[slen] = '\0'; + dynarray_append(list, copy); +} + +void strlist_append_dup( strlist_t *list, const char *str) +{ + strlist_append_b(list, str, strlen(str)); +} + +void strlist_done( strlist_t *list ) +{ + STRLIST_FOREACH(list, string, free(string)); + dynarray_done(list); +} + +static int strlist_compare_strings(const void* a, const void* b) +{ + const char *sa = *(const char **)a; + const char *sb = *(const char **)b; + return strcmp(sa, sb); +} + +void strlist_sort( strlist_t *list ) +{ + if (list->count > 0) { + qsort(list->items, + (size_t)list->count, + sizeof(void*), + strlist_compare_strings); + } +} diff --git a/toolbox/dynarray.h b/toolbox/dynarray.h new file mode 100644 index 0000000..f73fb3b --- /dev/null +++ b/toolbox/dynarray.h @@ -0,0 +1,80 @@ +#ifndef DYNARRAY_H +#define DYNARRAY_H + +#include <stddef.h> + +/* simple dynamic array of pointers */ +typedef struct { + int count; + int capacity; + void** items; +} dynarray_t; + +#define DYNARRAY_INITIALIZER { 0, 0, NULL } + +void dynarray_init( dynarray_t *a ); +void dynarray_done( dynarray_t *a ); + +void dynarray_append( dynarray_t *a, void* item ); + +/* Used to iterate over a dynarray_t + * _array :: pointer to the array + * _item_type :: type of objects pointed to by the array + * _item :: name of a local variable defined within the loop + * with type '_item_type' + * _stmnt :: C statement that will be executed in each iteration. + * + * You case use 'break' and 'continue' within _stmnt + * + * This macro is only intended for simple uses. I.e. do not add or + * remove items from the array during iteration. + */ +#define DYNARRAY_FOREACH_TYPE(_array,_item_type,_item,_stmnt) \ + do { \ + int _nn_##__LINE__ = 0; \ + for (;_nn_##__LINE__ < (_array)->count; ++ _nn_##__LINE__) { \ + _item_type _item = (_item_type)(_array)->items[_nn_##__LINE__]; \ + _stmnt; \ + } \ + } while (0) + +#define DYNARRAY_FOREACH(_array,_item,_stmnt) \ + DYNARRAY_FOREACH_TYPE(_array,void *,_item,_stmnt) + +/* Simple dynamic string arrays + * + * NOTE: A strlist_t owns the strings it references. + */ +typedef dynarray_t strlist_t; + +#define STRLIST_INITIALIZER DYNARRAY_INITIALIZER + +/* Used to iterate over a strlist_t + * _list :: pointer to strlist_t object + * _string :: name of local variable name defined within the loop with + * type 'char*' + * _stmnt :: C statement executed in each iteration + * + * This macro is only intended for simple uses. Do not add or remove items + * to/from the list during iteration. + */ +#define STRLIST_FOREACH(_list,_string,_stmnt) \ + DYNARRAY_FOREACH_TYPE(_list,char *,_string,_stmnt) + +void strlist_init( strlist_t *list ); + +/* note: strlist_done will free all the strings owned by the list */ +void strlist_done( strlist_t *list ); + +/* append a new string made of the first 'slen' characters from 'str' + * followed by a trailing zero. + */ +void strlist_append_b( strlist_t *list, const void* str, size_t slen ); + +/* append the copy of a given input string to a strlist_t */ +void strlist_append_dup( strlist_t *list, const char *str); + +/* sort the strings in a given list (using strcmp) */ +void strlist_sort( strlist_t *list ); + +#endif /* DYNARRAY_H */
\ No newline at end of file diff --git a/toolbox/getprop.c b/toolbox/getprop.c index fc80a4d..c001fda 100644 --- a/toolbox/getprop.c +++ b/toolbox/getprop.c @@ -1,13 +1,34 @@ #include <stdio.h> +#include <stdlib.h> #include <cutils/properties.h> #include <sys/system_properties.h> +#include "dynarray.h" -static void proplist(const char *key, const char *name, - void *user __attribute__((unused))) +static void record_prop(const char* key, const char* name, void* opaque) { - printf("[%s]: [%s]\n", key, name); + strlist_t* list = opaque; + char temp[PROP_VALUE_MAX + PROP_NAME_MAX + 16]; + snprintf(temp, sizeof temp, "[%s]: [%s]", key, name); + strlist_append_dup(list, temp); +} + +static void list_properties(void) +{ + strlist_t list[1] = { STRLIST_INITIALIZER }; + + /* Record properties in the string list */ + (void)property_list(record_prop, list); + + /* Sort everything */ + strlist_sort(list); + + /* print everything */ + STRLIST_FOREACH(list, str, printf("%s\n", str)); + + /* voila */ + strlist_done(list); } int __system_property_wait(prop_info *pi); @@ -17,7 +38,7 @@ int getprop_main(int argc, char *argv[]) int n = 0; if (argc == 1) { - (void)property_list(proplist, NULL); + list_properties(); } else { char value[PROPERTY_VALUE_MAX]; char *default_value; diff --git a/toolbox/hd.c b/toolbox/hd.c index 1f7d179..da31245 100644 --- a/toolbox/hd.c +++ b/toolbox/hd.c @@ -2,6 +2,7 @@ #include <stdlib.h> #include <string.h> #include <stdint.h> +#include <unistd.h> #include <fcntl.h> #include <sys/ioctl.h> #include <errno.h> diff --git a/toolbox/ioctl.c b/toolbox/ioctl.c index e28f2a4..fb555d2 100644 --- a/toolbox/ioctl.c +++ b/toolbox/ioctl.c @@ -1,11 +1,14 @@ #include <stdio.h> #include <stdlib.h> +#include <stdint.h> #include <fcntl.h> +#include <getopt.h> #include <string.h> #include <linux/kd.h> #include <linux/vt.h> #include <errno.h> #include <pthread.h> +#include <sys/ioctl.h> int ioctl_main(int argc, char *argv[]) { diff --git a/toolbox/ls.c b/toolbox/ls.c index 962bf47..b08e378 100644 --- a/toolbox/ls.c +++ b/toolbox/ls.c @@ -15,128 +15,7 @@ #include <linux/kdev_t.h> #include <limits.h> -// dynamic arrays -typedef struct { - int count; - int capacity; - void** items; -} dynarray_t; - -#define DYNARRAY_INITIALIZER { 0, 0, NULL } - -static void dynarray_init( dynarray_t *a ) -{ - a->count = a->capacity = 0; - a->items = NULL; -} - -static void dynarray_reserve_more( dynarray_t *a, int count ) -{ - int old_cap = a->capacity; - int new_cap = old_cap; - const int max_cap = INT_MAX/sizeof(void*); - void** new_items; - int new_count = a->count + count; - - if (count <= 0) - return; - - if (count > max_cap - a->count) - abort(); - - new_count = a->count + count; - - while (new_cap < new_count) { - old_cap = new_cap; - new_cap += (new_cap >> 2) + 4; - if (new_cap < old_cap || new_cap > max_cap) { - new_cap = max_cap; - } - } - new_items = realloc(a->items, new_cap*sizeof(void*)); - if (new_items == NULL) - abort(); - - a->items = new_items; - a->capacity = new_cap; -} - -static void dynarray_append( dynarray_t *a, void* item ) -{ - if (a->count >= a->capacity) - dynarray_reserve_more(a, 1); - - a->items[a->count++] = item; -} - -static void dynarray_done( dynarray_t *a ) -{ - free(a->items); - a->items = NULL; - a->count = a->capacity = 0; -} - -#define DYNARRAY_FOREACH_TYPE(_array,_item_type,_item,_stmnt) \ - do { \ - int _nn_##__LINE__ = 0; \ - for (;_nn_##__LINE__ < (_array)->count; ++ _nn_##__LINE__) { \ - _item_type _item = (_item_type)(_array)->items[_nn_##__LINE__]; \ - _stmnt; \ - } \ - } while (0) - -#define DYNARRAY_FOREACH(_array,_item,_stmnt) \ - DYNARRAY_FOREACH_TYPE(_array,void *,_item,_stmnt) - -// string arrays - -typedef dynarray_t strlist_t; - -#define STRLIST_INITIALIZER DYNARRAY_INITIALIZER - -#define STRLIST_FOREACH(_list,_string,_stmnt) \ - DYNARRAY_FOREACH_TYPE(_list,char *,_string,_stmnt) - -static void strlist_init( strlist_t *list ) -{ - dynarray_init(list); -} - -static void strlist_append_b( strlist_t *list, const void* str, size_t slen ) -{ - char *copy = malloc(slen+1); - memcpy(copy, str, slen); - copy[slen] = '\0'; - dynarray_append(list, copy); -} - -static void strlist_append_dup( strlist_t *list, const char *str) -{ - strlist_append_b(list, str, strlen(str)); -} - -static void strlist_done( strlist_t *list ) -{ - STRLIST_FOREACH(list, string, free(string)); - dynarray_done(list); -} - -static int strlist_compare_strings(const void* a, const void* b) -{ - const char *sa = *(const char **)a; - const char *sb = *(const char **)b; - return strcmp(sa, sb); -} - -static void strlist_sort( strlist_t *list ) -{ - if (list->count > 0) { - qsort(list->items, - (size_t)list->count, - sizeof(void*), - strlist_compare_strings); - } -} +#include "dynarray.h" // bits for flags argument #define LIST_LONG (1 << 0) @@ -144,6 +23,7 @@ static void strlist_sort( strlist_t *list ) #define LIST_RECURSIVE (1 << 2) #define LIST_DIRECTORIES (1 << 3) #define LIST_SIZE (1 << 4) +#define LIST_LONG_NUMERIC (1 << 5) // fwd static int listpath(const char *name, int flags); @@ -165,7 +45,7 @@ static char mode2kind(unsigned mode) static void mode2str(unsigned mode, char *out) { *out++ = mode2kind(mode); - + *out++ = (mode & 0400) ? 'r' : '-'; *out++ = (mode & 0200) ? 'w' : '-'; if(mode & 04000) { @@ -279,12 +159,17 @@ static int listfile_long(const char *path, int flags) } mode2str(s.st_mode, mode); - user2str(s.st_uid, user); - group2str(s.st_gid, group); + if (flags & LIST_LONG_NUMERIC) { + sprintf(user, "%ld", s.st_uid); + sprintf(group, "%ld", s.st_gid); + } else { + user2str(s.st_uid, user); + group2str(s.st_gid, group); + } strftime(date, 32, "%Y-%m-%d %H:%M", localtime((const time_t*)&s.st_mtime)); date[31] = 0; - + // 12345678901234567890123456789012345678901234567890123456789012345678901234567890 // MMMMMMMM UUUUUUUU GGGGGGGGG XXXXXXXX YYYY-MM-DD HH:MM NAME (->LINK) @@ -292,7 +177,7 @@ static int listfile_long(const char *path, int flags) case S_IFBLK: case S_IFCHR: printf("%s %-8s %-8s %3d, %3d %s %s\n", - mode, user, group, + mode, user, group, (int) MAJOR(s.st_rdev), (int) MINOR(s.st_rdev), date, name); break; @@ -306,7 +191,7 @@ static int listfile_long(const char *path, int flags) len = readlink(path, linkto, 256); if(len < 0) return -1; - + if(len > 255) { linkto[252] = '.'; linkto[253] = '.'; @@ -315,7 +200,7 @@ static int listfile_long(const char *path, int flags) } else { linkto[len] = 0; } - + printf("%s %-8s %-8s %s %s -> %s\n", mode, user, group, date, name, linkto); break; @@ -358,7 +243,7 @@ static int listdir(const char *name, int flags) DIR *d; struct dirent *de; strlist_t files = STRLIST_INITIALIZER; - + d = opendir(name); if(d == 0) { fprintf(stderr, "opendir failed, %s\n", strerror(errno)); @@ -463,7 +348,7 @@ int ls_main(int argc, char **argv) { int flags = 0; int listed = 0; - + if(argc > 1) { int i; int err = 0; @@ -476,6 +361,7 @@ int ls_main(int argc, char **argv) while (arg[0]) { switch (arg[0]) { case 'l': flags |= LIST_LONG; break; + case 'n': flags |= LIST_LONG | LIST_LONG_NUMERIC; break; case 's': flags |= LIST_SIZE; break; case 'R': flags |= LIST_RECURSIVE; break; case 'd': flags |= LIST_DIRECTORIES; break; @@ -502,7 +388,7 @@ int ls_main(int argc, char **argv) return err; } } - - // list working directory if no files or directories were specified + + // list working directory if no files or directories were specified return listpath(".", flags); } diff --git a/toolbox/lsof.c b/toolbox/lsof.c index 99891db..c55384b 100644 --- a/toolbox/lsof.c +++ b/toolbox/lsof.c @@ -196,28 +196,37 @@ void lsof_dumpinfo(pid_t pid) int lsof_main(int argc, char *argv[]) { - DIR *dir = opendir("/proc"); - if (dir == NULL) { - fprintf(stderr, "Couldn't open /proc\n"); - return -1; + long int pid = 0; + char* endptr; + if (argc == 2) { + pid = strtol(argv[1], &endptr, 10); } print_header(); - struct dirent* de; - while ((de = readdir(dir))) { - if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) - continue; - - // Only inspect directories that are PID numbers - char* endptr; - long int pid = strtol(de->d_name, &endptr, 10); - if (*endptr != '\0') - continue; - + if (pid) { lsof_dumpinfo(pid); + } else { + DIR *dir = opendir("/proc"); + if (dir == NULL) { + fprintf(stderr, "Couldn't open /proc\n"); + return -1; + } + + struct dirent* de; + while ((de = readdir(dir))) { + if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) + continue; + + // Only inspect directories that are PID numbers + pid = strtol(de->d_name, &endptr, 10); + if (*endptr != '\0') + continue; + + lsof_dumpinfo(pid); + } + closedir(dir); } - closedir(dir); return 0; } diff --git a/toolbox/lsusb.c b/toolbox/lsusb.c new file mode 100644 index 0000000..236e74b --- /dev/null +++ b/toolbox/lsusb.c @@ -0,0 +1,227 @@ +/* + * Copyright (C) 2010 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <endian.h> +#include <errno.h> +#include <stdio.h> +#include <stdint.h> +#include <string.h> + +#include <usbhost/usbhost.h> + +static int verbose = 0; +static char str_buff[4096]; + +static const char *get_str(struct usb_device *dev, int id) +{ + char *str = usb_device_get_string(dev, id); + + if (id && str) { + strlcpy(str_buff, str, sizeof(str_buff)); + free(str); + } else { + snprintf(str_buff, sizeof(str_buff), "%02x", id); + } + + return str_buff; +} + + +static void lsusb_parse_device_descriptor(struct usb_device *dev, + struct usb_device_descriptor *desc) +{ + printf(" Device Descriptor\n"); + printf("\tbcdUSB: %04x\n", letoh16(desc->bcdUSB)); + printf("\tbDeviceClass: %02x\n", desc->bDeviceClass); + printf("\tbDeviceSubClass: %02x\n", desc->bDeviceSubClass); + printf("\tbDeviceProtocol: %02x\n", desc->bDeviceProtocol); + printf("\tbMaxPacketSize0: %02x\n", desc->bMaxPacketSize0); + printf("\tidVendor: %04x\n", letoh16(desc->idVendor)); + printf("\tidProduct: %04x\n", letoh16(desc->idProduct)); + printf("\tbcdDevice: %04x\n", letoh16(desc->bcdDevice)); + printf("\tiManufacturer: %s\n", get_str(dev, desc->iManufacturer)); + printf("\tiProduct: %s\n", get_str(dev, desc->iProduct)); + printf("\tiSerialNumber: %s\n", get_str(dev,desc->iSerialNumber)); + printf("\tbNumConfiguration: %02x\n", desc->bNumConfigurations); + printf("\n"); +} + +static void lsusb_parse_config_descriptor(struct usb_device *dev, + struct usb_config_descriptor *desc) +{ + printf(" Config Descriptor\n"); + printf("\twTotalLength: %04x\n", letoh16(desc->wTotalLength)); + printf("\tbNumInterfaces: %02x\n", desc->bNumInterfaces); + printf("\tbConfigurationValue: %02x\n", desc->bConfigurationValue); + printf("\tiConfiguration: %s\n", get_str(dev, desc->iConfiguration)); + printf("\tbmAttributes: %02x\n", desc->bmAttributes); + printf("\tbMaxPower: %d mA\n", desc->bMaxPower * 2); + printf("\n"); +} + +static void lsusb_parse_interface_descriptor(struct usb_device *dev, + struct usb_interface_descriptor *desc) +{ + printf(" Interface Descriptor\n"); + printf("\tbInterfaceNumber: %02x\n", desc->bInterfaceNumber); + printf("\tbAlternateSetting: %02x\n", desc->bAlternateSetting); + printf("\tbNumEndpoints: %02x\n", desc->bNumEndpoints); + printf("\tbInterfaceClass: %02x\n", desc->bInterfaceClass); + printf("\tbInterfaceSubClass: %02x\n", desc->bInterfaceSubClass); + printf("\tbInterfaceProtocol: %02x\n", desc->bInterfaceProtocol); + printf("\tiInterface: %s\n", get_str(dev, desc->iInterface)); + printf("\n"); +} + +static void lsusb_parse_endpoint_descriptor(struct usb_device *dev, + struct usb_endpoint_descriptor *desc) +{ + printf(" Endpoint Descriptor\n"); + printf("\tbEndpointAddress: %02x\n", desc->bEndpointAddress); + printf("\tbmAttributes: %02x\n", desc->bmAttributes); + printf("\twMaxPacketSize: %02x\n", letoh16(desc->wMaxPacketSize)); + printf("\tbInterval: %02x\n", desc->bInterval); + printf("\tbRefresh: %02x\n", desc->bRefresh); + printf("\tbSynchAddress: %02x\n", desc->bSynchAddress); + printf("\n"); +} + +static void lsusb_dump_descriptor(struct usb_device *dev, + struct usb_descriptor_header *desc) +{ + int i; + printf(" Descriptor type %02x\n", desc->bDescriptorType); + + for (i = 0; i < desc->bLength; i++ ) { + if ((i % 16) == 0) + printf("\t%02x:", i); + printf(" %02x", ((uint8_t *)desc)[i]); + if ((i % 16) == 15) + printf("\n"); + } + + if ((i % 16) != 0) + printf("\n"); + printf("\n"); +} + +static void lsusb_parse_descriptor(struct usb_device *dev, + struct usb_descriptor_header *desc) +{ + switch (desc->bDescriptorType) { + case USB_DT_DEVICE: + lsusb_parse_device_descriptor(dev, (struct usb_device_descriptor *) desc); + break; + + case USB_DT_CONFIG: + lsusb_parse_config_descriptor(dev, (struct usb_config_descriptor *) desc); + break; + + case USB_DT_INTERFACE: + lsusb_parse_interface_descriptor(dev, (struct usb_interface_descriptor *) desc); + break; + + case USB_DT_ENDPOINT: + lsusb_parse_endpoint_descriptor(dev, (struct usb_endpoint_descriptor *) desc); + break; + + default: + lsusb_dump_descriptor(dev, desc); + + break; + } +} + +static int lsusb_device_added(const char *dev_name, void *client_data) +{ + struct usb_device *dev = usb_device_open(dev_name); + + if (!dev) { + fprintf(stderr, "can't open device %s: %s\n", dev_name, strerror(errno)); + return 0; + } + + if (verbose) { + struct usb_descriptor_iter iter; + struct usb_descriptor_header *desc; + + printf("%s:\n", dev_name); + + usb_descriptor_iter_init(dev, &iter); + + while ((desc = usb_descriptor_iter_next(&iter)) != NULL) + lsusb_parse_descriptor(dev, desc); + + } else { + uint16_t vid, pid; + char *mfg_name, *product_name, *serial; + + vid = usb_device_get_vendor_id(dev); + pid = usb_device_get_product_id(dev); + mfg_name = usb_device_get_manufacturer_name(dev); + product_name = usb_device_get_product_name(dev); + serial = usb_device_get_serial(dev); + + printf("%s: %04x:%04x %s %s %s\n", dev_name, vid, pid, + mfg_name, product_name, serial); + + free(mfg_name); + free(product_name); + free(serial); + } + + usb_device_close(dev); + + return 0; +} + +static int lsusb_device_removed(const char *dev_name, void *client_data) +{ + return 0; +} + + +static int lsusb_discovery_done(void *client_data) +{ + return 1; +} + + + +int lsusb_main(int argc, char **argv) +{ + struct usb_host_context *ctx; + + if (argc == 2 && !strcmp(argv[1], "-v")) + verbose = 1; + + ctx = usb_host_init(); + if (!ctx) { + perror("usb_host_init:"); + return 1; + } + + usb_host_run(ctx, + lsusb_device_added, + lsusb_device_removed, + lsusb_discovery_done, + NULL); + + usb_host_cleanup(ctx); + + return 0; +} + diff --git a/toolbox/mount.c b/toolbox/mount.c index 472c952..82ecc56 100644 --- a/toolbox/mount.c +++ b/toolbox/mount.c @@ -222,9 +222,50 @@ static int print_mounts() return 0; } +static int get_mounts_dev_dir(const char *arg, char **dev, char **dir) +{ + FILE *f; + char mount_dev[256]; + char mount_dir[256]; + char mount_type[256]; + char mount_opts[256]; + int mount_freq; + int mount_passno; + int match; + + f = fopen("/proc/mounts", "r"); + if (!f) { + fprintf(stdout, "could not open /proc/mounts\n"); + return -1; + } + + do { + match = fscanf(f, "%255s %255s %255s %255s %d %d\n", + mount_dev, mount_dir, mount_type, + mount_opts, &mount_freq, &mount_passno); + mount_dev[255] = 0; + mount_dir[255] = 0; + mount_type[255] = 0; + mount_opts[255] = 0; + if (match == 6 && + (strcmp(arg, mount_dev) == 0 || + strcmp(arg, mount_dir) == 0)) { + *dev = strdup(mount_dev); + *dir = strdup(mount_dir); + fclose(f); + return 0; + } + } while (match != EOF); + + fclose(f); + return -1; +} + int mount_main(int argc, char *argv[]) { char *type = NULL; + char *dev = NULL; + char *dir = NULL; int c; int loop = 0; @@ -265,12 +306,19 @@ int mount_main(int argc, char *argv[]) if (rwflag & MS_TYPE) type = "none"; - if (optind + 2 != argc || type == NULL) { + if (optind + 2 == argc) { + dev = argv[optind]; + dir = argv[optind + 1]; + } else if (optind + 1 == argc && rwflag & MS_REMOUNT) { + get_mounts_dev_dir(argv[optind], &dev, &dir); + } + + if (dev == NULL || dir == NULL || type == NULL) { fprintf(stderr, "Usage: %s [-r] [-w] [-o options] [-t type] " "device directory\n", progname); exit(1); } - return do_mount(argv[optind], argv[optind + 1], type, rwflag, - extra.str, loop); + return do_mount(dev, dir, type, rwflag, extra.str, loop); + /* We leak dev and dir in some cases, but we're about to exit */ } diff --git a/toolbox/nandread.c b/toolbox/nandread.c index 9644973..b124731 100644 --- a/toolbox/nandread.c +++ b/toolbox/nandread.c @@ -4,6 +4,7 @@ #include <ctype.h> #include <errno.h> #include <fcntl.h> +#include <unistd.h> #include <mtd/mtd-user.h> #include <sys/ioctl.h> @@ -283,4 +284,3 @@ int nandread_main(int argc, char **argv) return 0; } - diff --git a/toolbox/notify.c b/toolbox/notify.c index b1761d2..c983ed5 100644 --- a/toolbox/notify.c +++ b/toolbox/notify.c @@ -3,6 +3,7 @@ #include <string.h> #include <stdint.h> #include <fcntl.h> +#include <unistd.h> #include <sys/ioctl.h> #include <sys/inotify.h> #include <errno.h> diff --git a/toolbox/ps.c b/toolbox/ps.c index bc50cfa..2aa3efb 100644 --- a/toolbox/ps.c +++ b/toolbox/ps.c @@ -27,6 +27,7 @@ static char *nexttok(char **strp) #define SHOW_PRIO 1 #define SHOW_TIME 2 #define SHOW_POLICY 4 +#define SHOW_CPU 8 static int display_flags = 0; @@ -41,7 +42,7 @@ static int ps_line(int pid, int tid, char *namefilter) int ppid, tty; unsigned wchan, rss, vss, eip; unsigned utime, stime; - int prio, nice, rtprio, sched; + int prio, nice, rtprio, sched, psr; struct passwd *pw; sprintf(statline, "/proc/%d", pid); @@ -122,7 +123,7 @@ static int ps_line(int pid, int tid, char *namefilter) nexttok(&ptr); // nswap nexttok(&ptr); // cnswap nexttok(&ptr); // exit signal - nexttok(&ptr); // processor + psr = atoi(nexttok(&ptr)); // processor rtprio = atoi(nexttok(&ptr)); // rt_priority sched = atoi(nexttok(&ptr)); // scheduling policy @@ -142,7 +143,9 @@ static int ps_line(int pid, int tid, char *namefilter) if(!namefilter || !strncmp(name, namefilter, strlen(namefilter))) { printf("%-9s %-5d %-5d %-6d %-5d", user, pid, ppid, vss / 1024, rss * 4); - if(display_flags&SHOW_PRIO) + if (display_flags & SHOW_CPU) + printf(" %-2d", psr); + if (display_flags & SHOW_PRIO) printf(" %-5d %-5d %-5d %-5d", prio, nice, rtprio, sched); if (display_flags & SHOW_POLICY) { SchedPolicy p; @@ -207,6 +210,8 @@ int ps_main(int argc, char **argv) display_flags |= SHOW_POLICY; } else if(!strcmp(argv[1],"-p")) { display_flags |= SHOW_PRIO; + } else if(!strcmp(argv[1],"-c")) { + display_flags |= SHOW_CPU; } else if(isdigit(argv[1][0])){ pidfilter = atoi(argv[1]); } else { @@ -216,7 +221,8 @@ int ps_main(int argc, char **argv) argv++; } - printf("USER PID PPID VSIZE RSS %s %s WCHAN PC NAME\n", + printf("USER PID PPID VSIZE RSS %s%s %s WCHAN PC NAME\n", + (display_flags&SHOW_CPU)?"CPU ":"", (display_flags&SHOW_PRIO)?"PRIO NICE RTPRI SCHED ":"", (display_flags&SHOW_POLICY)?"PCY " : ""); while((de = readdir(d)) != 0){ diff --git a/toolbox/r.c b/toolbox/r.c index 5a82e20..eb8ea0b 100644 --- a/toolbox/r.c +++ b/toolbox/r.c @@ -13,8 +13,10 @@ static int usage() int r_main(int argc, char *argv[]) { int width = 4, set = 0, fd; - unsigned addr, value; + unsigned addr, value, endaddr = 0; + unsigned long mmap_start, mmap_size; void *page; + char *end; if(argc < 2) return usage(); @@ -31,6 +33,18 @@ int r_main(int argc, char *argv[]) if(argc < 2) return usage(); addr = strtoul(argv[1], 0, 16); + end = strchr(argv[1], '-'); + if (end) + endaddr = strtoul(end + 1, 0, 16); + + if (!endaddr) + endaddr = addr + width - 1; + + if (endaddr <= addr) { + fprintf(stderr, "invalid end address\n"); + return -1; + } + if(argc > 2) { set = 1; value = strtoul(argv[2], 0, 16); @@ -42,33 +56,40 @@ int r_main(int argc, char *argv[]) return -1; } - page = mmap(0, 8192, PROT_READ | PROT_WRITE, - MAP_SHARED, fd, addr & (~4095)); + mmap_start = addr & ~(PAGE_SIZE - 1); + mmap_size = endaddr - mmap_start + 1; + mmap_size = (mmap_size + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1); + + page = mmap(0, mmap_size, PROT_READ | PROT_WRITE, + MAP_SHARED, fd, mmap_start); if(page == MAP_FAILED){ fprintf(stderr,"cannot mmap region\n"); return -1; } - switch(width){ - case 4: { - unsigned *x = (unsigned*) (((unsigned) page) + (addr & 4095)); - if(set) *x = value; - fprintf(stderr,"%08x: %08x\n", addr, *x); - break; - } - case 2: { - unsigned short *x = (unsigned short*) (((unsigned) page) + (addr & 4095)); - if(set) *x = value; - fprintf(stderr,"%08x: %04x\n", addr, *x); - break; - } - case 1: { - unsigned char *x = (unsigned char*) (((unsigned) page) + (addr & 4095)); - if(set) *x = value; - fprintf(stderr,"%08x: %02x\n", addr, *x); - break; + while (addr <= endaddr) { + switch(width){ + case 4: { + unsigned *x = (unsigned*) (((unsigned) page) + (addr & 4095)); + if(set) *x = value; + fprintf(stderr,"%08x: %08x\n", addr, *x); + break; + } + case 2: { + unsigned short *x = (unsigned short*) (((unsigned) page) + (addr & 4095)); + if(set) *x = value; + fprintf(stderr,"%08x: %04x\n", addr, *x); + break; + } + case 1: { + unsigned char *x = (unsigned char*) (((unsigned) page) + (addr & 4095)); + if(set) *x = value; + fprintf(stderr,"%08x: %02x\n", addr, *x); + break; + } + } + addr += width; } - } return 0; } diff --git a/toolbox/reboot.c b/toolbox/reboot.c index aebe185..f8546de 100644 --- a/toolbox/reboot.c +++ b/toolbox/reboot.c @@ -1,7 +1,7 @@ #include <errno.h> #include <stdio.h> #include <stdlib.h> -#include <sys/reboot.h> +#include <cutils/android_reboot.h> #include <unistd.h> int reboot_main(int argc, char *argv[]) @@ -9,6 +9,7 @@ int reboot_main(int argc, char *argv[]) int ret; int nosync = 0; int poweroff = 0; + int flags = 0; opterr = 0; do { @@ -38,15 +39,16 @@ int reboot_main(int argc, char *argv[]) exit(EXIT_FAILURE); } - if(!nosync) - sync(); + if(nosync) + /* also set NO_REMOUNT_RO as remount ro includes an implicit sync */ + flags = ANDROID_RB_FLAG_NO_SYNC | ANDROID_RB_FLAG_NO_REMOUNT_RO; if(poweroff) - ret = __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_POWER_OFF, NULL); + ret = android_reboot(ANDROID_RB_POWEROFF, flags, 0); else if(argc > optind) - ret = __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, argv[optind]); + ret = android_reboot(ANDROID_RB_RESTART2, flags, argv[optind]); else - ret = reboot(RB_AUTOBOOT); + ret = android_reboot(ANDROID_RB_RESTART, flags, 0); if(ret < 0) { perror("reboot"); exit(EXIT_FAILURE); diff --git a/toolbox/schedtop.c b/toolbox/schedtop.c index c0e0141..6859b50 100644 --- a/toolbox/schedtop.c +++ b/toolbox/schedtop.c @@ -2,7 +2,9 @@ #include <stdlib.h> #include <ctype.h> #include <fcntl.h> +#include <unistd.h> +#include <stdint.h> #include <string.h> #include <sys/stat.h> @@ -332,4 +334,3 @@ int schedtop_main(int argc, char **argv) closedir(d); return 0; } - diff --git a/toolbox/setconsole.c b/toolbox/setconsole.c index b0ce13f..0159c07 100644 --- a/toolbox/setconsole.c +++ b/toolbox/setconsole.c @@ -6,6 +6,8 @@ #include <linux/vt.h> #include <errno.h> #include <pthread.h> +#include <unistd.h> +#include <sys/ioctl.h> static int activate_thread_switch_vc; static void *activate_thread(void *arg) diff --git a/toolbox/top.c b/toolbox/top.c index daade6d..999c8e1 100644 --- a/toolbox/top.c +++ b/toolbox/top.c @@ -65,6 +65,7 @@ struct proc_info { long unsigned delta_time; long vss; long rss; + int prs; int num_threads; char policy[32]; }; @@ -340,8 +341,9 @@ static int read_stat(char *filename, struct proc_info *proc) { /* Scan rest of string. */ sscanf(close_paren + 1, " %c %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d " - "%lu %lu %*d %*d %*d %*d %*d %*d %*d %lu %ld", - &proc->state, &proc->utime, &proc->stime, &proc->vss, &proc->rss); + "%lu %lu %*d %*d %*d %*d %*d %*d %*d %lu %ld " + "%*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %d", + &proc->state, &proc->utime, &proc->stime, &proc->vss, &proc->rss, &proc->prs); return 0; } @@ -454,9 +456,9 @@ static void print_procs(void) { total_delta_time); printf("\n"); if (!threads) - printf("%5s %4s %1s %5s %7s %7s %3s %-8s %s\n", "PID", "CPU%", "S", "#THR", "VSS", "RSS", "PCY", "UID", "Name"); + printf("%5s %2s %4s %1s %5s %7s %7s %3s %-8s %s\n", "PID", "PR", "CPU%", "S", "#THR", "VSS", "RSS", "PCY", "UID", "Name"); else - printf("%5s %5s %4s %1s %7s %7s %3s %-8s %-15s %s\n", "PID", "TID", "CPU%", "S", "VSS", "RSS", "PCY", "UID", "Thread", "Proc"); + printf("%5s %5s %2s %4s %1s %7s %7s %3s %-8s %-15s %s\n", "PID", "TID", "PR", "CPU%", "S", "VSS", "RSS", "PCY", "UID", "Thread", "Proc"); for (i = 0; i < num_new_procs; i++) { proc = new_procs[i]; @@ -478,10 +480,10 @@ static void print_procs(void) { group_str = group_buf; } if (!threads) - printf("%5d %3ld%% %c %5d %6ldK %6ldK %3s %-8.8s %s\n", proc->pid, proc->delta_time * 100 / total_delta_time, proc->state, proc->num_threads, + printf("%5d %2d %3ld%% %c %5d %6ldK %6ldK %3s %-8.8s %s\n", proc->pid, proc->prs, proc->delta_time * 100 / total_delta_time, proc->state, proc->num_threads, proc->vss / 1024, proc->rss * getpagesize() / 1024, proc->policy, user_str, proc->name[0] != 0 ? proc->name : proc->tname); else - printf("%5d %5d %3ld%% %c %6ldK %6ldK %3s %-8.8s %-15s %s\n", proc->pid, proc->tid, proc->delta_time * 100 / total_delta_time, proc->state, + printf("%5d %5d %2d %3ld%% %c %6ldK %6ldK %3s %-8.8s %-15s %s\n", proc->pid, proc->tid, proc->prs, proc->delta_time * 100 / total_delta_time, proc->state, proc->vss / 1024, proc->rss * getpagesize() / 1024, proc->policy, user_str, proc->tname, proc->name); } } diff --git a/toolbox/uptime.c b/toolbox/uptime.c index 3d8061c..1c312b0 100644 --- a/toolbox/uptime.c +++ b/toolbox/uptime.c @@ -35,6 +35,7 @@ #include <linux/android_alarm.h> #include <fcntl.h> #include <stdio.h> +#include <time.h> static void format_time(int time, char* buffer) { @@ -75,19 +76,26 @@ int uptime_main(int argc, char *argv[]) float up_time, idle_time; char up_string[100], idle_string[100], sleep_string[100]; int elapsed; + struct timespec up_timespec; FILE* file = fopen("/proc/uptime", "r"); if (!file) { fprintf(stderr, "Could not open /proc/uptime\n"); return -1; } - if (fscanf(file, "%f %f", &up_time, &idle_time) != 2) { + if (fscanf(file, "%*f %f", &idle_time) != 1) { fprintf(stderr, "Could not parse /proc/uptime\n"); fclose(file); return -1; } fclose(file); + if (clock_gettime(CLOCK_MONOTONIC, &up_timespec) < 0) { + fprintf(stderr, "Could not get monotonic time\n"); + return -1; + } + up_time = up_timespec.tv_sec + up_timespec.tv_nsec / 1e9; + elapsed = elapsedRealtime(); if (elapsed < 0) { fprintf(stderr, "elapsedRealtime failed\n"); diff --git a/toolbox/wipe.c b/toolbox/wipe.c index 7e263fd..650a0d6 100644 --- a/toolbox/wipe.c +++ b/toolbox/wipe.c @@ -5,7 +5,7 @@ #include <string.h> #include <errno.h> #include <sys/types.h> -#include <sys/reboot.h> +#include <cutils/android_reboot.h> #include <sys/stat.h> #ifndef PATH_MAX @@ -63,7 +63,7 @@ int wipe_main (int argc, char *argv[]) wipe ("/system"); wipe ("/data"); fprintf(stdout, "Device nuked! Rebooting...\n"); - ret = reboot(RB_AUTOBOOT); + ret = android_reboot(ANDROID_RB_RESTART, 0, 0); if (ret < 0) { fprintf(stderr, "Reboot failed, %s\n", strerror(errno)); return 1; |